R 中的回归树
我在 R 中制作回归树时遇到问题。我有一个包含 17 个属性的数据框,
library(rpart)
rt.model <- rpart(razlika ~ ., learn)
但出现错误:
Error in `[.data.frame`(frame, predictors) : undefined columns selected
看起来很奇怪,因为我用一个非常相似的数据库做了类似的事情。您可以在 http://uploading.com/files/de8a966d/exa.Rda/ 上下载数据框 - 然后加载
load("exa.Rda")
I am having trouble making a regression tree in R. I have a data frame with 17 attributes
library(rpart)
rt.model <- rpart(razlika ~ ., learn)
I get an error:
Error in `[.data.frame`(frame, predictors) : undefined columns selected
Seems weird because I did something like that with a very simillar database. You can dowload the dataframe on http://uploading.com/files/de8a966d/exa.Rda/ -
then load with
load("exa.Rda")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相信,问题不在于你有一个矩阵而不是一个数据框。当我下载并加载数据集时,我得到一个数据框,而不是矩阵。
问题是列名中存在错误字符。使用
gsub
从列名称中删除字符“-”、“”、“(”和“)”。或者您可以完全使用colnames
自己重新定义列名称。或者像 ulvund 一样,简单地调用
data.frame
,默认情况下,这会强制 R 为您执行列名清理。当我这样做时,
rpart
运行得很好。The problem is not, I believe, that you have a matrix rather than a data frame. When I download and then load you data set, I get a data frame, not a matrix.
The problem is that you have bad characters in the column names. Use
gsub
to remove the characters "-", " ", "(" and ")" from the column names. Or you can simply redefine the column names yourself entirely usingcolnames
.Or do as ulvund does and simply call
data.frame
, which forces R to do the column name cleaning for you, by default.When I do this,
rpart
runs just fine.将您的学习矩阵转换为数据框。
示例:
产量:
Turn your learn matrix into a data frame.
Example:
yields:
如果列名称是整数
(1:N)
,即使它们存储为字符,也会发生这种情况。This can also happen if column names are integers
(1:N)
, even though they are stored as characters.