Error in eval(expr, envir, enclos) : objec

2019-01-21 23:55发布

I cannot understand what is going wrong here.

data.train <- read.table("Assign2.WineComplete.csv",sep=",",header=T)
# Building decision tree
Train <- data.frame(residual.sugar=data.train$residual.sugar,
                total.sulfur.dioxide=data.train$total.sulfur.dioxide, 
                alcohol=data.train$alcohol,
                quality=data.train$quality)
Pre <- as.formula("pre ~ quality")

fit <- rpart(Pre, method="class",data=Train)

I am getting the following error :

Error in eval(expr, envir, enclos) : object 'pre' not found

4条回答
欢心
2楼-- · 2019-01-22 00:21

Just to add to this; This can happen if you don't attach your dataset Just wasted a half hour figuring this out as well.

Cheers

查看更多
可以哭但决不认输i
3楼-- · 2019-01-22 00:22

i use colname(train) = paste("A", colname(train)) and it turns out to the same problem as yours.

I finally figure out that randomForest is more stingy than rpart, it can't recognize the colname with space, comma or other specific punctuation.

paste function will prepend "A" and " " as seperator with each colname. so we need to avert the space and use this sentence instead:

colname(train) = paste("A", colname(train), sep = "")

this will prepend string without space.

查看更多
等我变得足够好
4楼-- · 2019-01-22 00:33

I think I got what I was looking for..

data.train <- read.table("Assign2.WineComplete.csv",sep=",",header=T)
fit <- rpart(quality ~ ., method="class",data=data.train)
plot(fit)
text(fit, use.n=TRUE)
summary(fit)
查看更多
走好不送
5楼-- · 2019-01-22 00:37

Don't know why @Janos deleted his answer, but it's correct: your data frame Train doesn't have a column named pre. When you pass a formula and a data frame to a model-fitting function, the names in the formula have to refer to columns in the data frame. Your Train has columns called residual.sugar, total.sulfur, alcohol and quality. You need to change either your formula or your data frame so they're consistent with each other.

And just to clarify: Pre is an object containing a formula. That formula contains a reference to the variable pre. It's the latter that has to be consistent with the data frame.

查看更多
登录 后发表回答