【发布时间】:2021-03-18 03:35:17
【问题描述】:
我正在尝试使用具有 200 多个变量的数据集创建决策树模型,但在使用 .所有变量的符号。
fit <- rpart(first_party ~ ., data=train)
Error in `[.data.frame`(m, labs) : undefined columns selected
当我按名称定义变量时,树模型很好,但我不能这样做 200 次。
fit <- rpart(first_party ~ Crime + Number_of_Schools + Pop_Total, data=train)
【问题讨论】:
-
如果你运行
names(train),结果向量中会弹出什么奇怪的东西吗? -
不,不是。我只是得到一长串变量:
[1] "Number_of_Independent_Schools" [2] "Number_of_Schools" [3] "Index of Multiple Deprivation" [4] "Income deprivation"等 -
名称中的空格可能会造成问题。运行
names(train) <- stringr::str_replace_all(names(train), " ", "_")并重试。 -
不是这样,而是另一个变量有一个\ in,这似乎是问题所在。谢谢