eval(expr, envir, enclos)中的错误:未找到的对象。

时间:2022-12-24 19:36:56

I am very new to coding in R, and cannot understand what is going wrong here. Any help will be much appreciated.

我对R的编码很陌生,不知道这里出了什么问题。非常感谢您的帮助。

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 个解决方案

#1


15  

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.

不知道为什么@Janos删除了他的答案,但它是正确的:你的数据帧序列没有一个名为pre的列。当您将一个公式和一个数据帧传递给模型拟合函数时,公式中的名称必须引用数据框架中的列。你的火车上有一列叫做“剩余”的列。糖,总。硫、酒精和质量。你需要改变你的公式或者你的数据框架,所以它们是一致的。

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.

澄清一下:Pre是一个包含公式的对象。该公式包含对变量pre的引用。后者必须与数据帧一致。

#2


6  

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

干杯

#3


0  

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)

#4


0  

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

我使用colname(火车)=粘贴(“A”,colname(火车)),结果和你的一样。

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

我最终发现,随机森林比rpart更吝啬,它不能识别出带有空格、逗号或其他特定标点符号的colname。

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

粘贴函数将prepend“A”和“”作为每个colname的seperator。所以我们需要避免使用这个句子:

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

this will prepend string without space.

这将prepend字符串没有空格。

#1


15  

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.

不知道为什么@Janos删除了他的答案,但它是正确的:你的数据帧序列没有一个名为pre的列。当您将一个公式和一个数据帧传递给模型拟合函数时,公式中的名称必须引用数据框架中的列。你的火车上有一列叫做“剩余”的列。糖,总。硫、酒精和质量。你需要改变你的公式或者你的数据框架,所以它们是一致的。

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.

澄清一下:Pre是一个包含公式的对象。该公式包含对变量pre的引用。后者必须与数据帧一致。

#2


6  

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

干杯

#3


0  

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)

#4


0  

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

我使用colname(火车)=粘贴(“A”,colname(火车)),结果和你的一样。

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

我最终发现,随机森林比rpart更吝啬,它不能识别出带有空格、逗号或其他特定标点符号的colname。

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

粘贴函数将prepend“A”和“”作为每个colname的seperator。所以我们需要避免使用这个句子:

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

this will prepend string without space.

这将prepend字符串没有空格。