eval(expr、envir、enclose)中出错:找不到对象“V2”

eval(expr、envir、enclose)中出错:找不到对象“V2”,r,R,为什么我会犯这个错误? 看起来我的svm分类器是基于输出工作的,但是其余的都不工作。我用R中给定的iris数据集尝试了这一点,下面的代码工作得非常好。但是,当尝试使用加载的数据集运行时,它不会 错误: Error in eval(expr, envir, enclos) : object 'V2' not found 代码: 结构 > str(balance_data) 'data.frame': 625 obs. of 5 variables: $ V1: Factor w/

为什么我会犯这个错误? 看起来我的svm分类器是基于输出工作的,但是其余的都不工作。我用R中给定的iris数据集尝试了这一点,下面的代码工作得非常好。但是,当尝试使用加载的数据集运行时,它不会

错误:

Error in eval(expr, envir, enclos) : object 'V2' not found
代码:

结构

> str(balance_data)
'data.frame':   625 obs. of  5 variables:
 $ V1: Factor w/ 3 levels "B","L","R": 1 3 3 3 3 3 3 3 3 3 ...
 $ V2: int  1 1 1 1 1 1 1 1 1 1 ...
 $ V3: int  1 1 1 1 1 1 1 1 1 1 ...
 $ V4: int  1 1 1 1 1 2 2 2 2 2 ...
 $ V5: int  1 2 3 4 5 1 2 3 4 5 ...
> str(X_train)
'data.frame':   500 obs. of  4 variables:
 $ V2: int  1 1 1 1 1 1 1 1 1 1 ...
 $ V3: int  1 1 1 1 1 1 1 1 1 1 ...
 $ V4: int  1 1 1 1 1 2 2 2 2 2 ...
 $ V5: int  1 2 3 4 5 1 2 3 4 5 ...
> str(X_test)
'data.frame':   122 obs. of  4 variables:
 $ V2: int  5 5 5 5 5 5 5 5 5 5 ...
 $ V3: int  1 1 1 1 1 1 1 1 1 1 ...
 $ V4: int  1 1 2 2 2 2 2 3 3 3 ...
 $ V5: int  4 5 1 2 3 4 5 1 2 3 ...
> str(Y_train)
 Factor w/ 3 levels "B","L","R": 1 3 3 3 3 3 3 3 3 3 ...
您的错误来自rpart函数

从rpart文档中:

用法

论据

公式: 一个公式,有响应但没有交互项。如果这是一个 数据来自E,作为模型框架,请参见model.frame

所以你需要一些类似的东西:

data_train <- head(balance_data,100)
...
tree_model <- rpart(V1 ~ V2 + V3 + V4, data_train) 
取决于你的型号

> str(balance_data)
'data.frame':   625 obs. of  5 variables:
 $ V1: Factor w/ 3 levels "B","L","R": 1 3 3 3 3 3 3 3 3 3 ...
 $ V2: int  1 1 1 1 1 1 1 1 1 1 ...
 $ V3: int  1 1 1 1 1 1 1 1 1 1 ...
 $ V4: int  1 1 1 1 1 2 2 2 2 2 ...
 $ V5: int  1 2 3 4 5 1 2 3 4 5 ...
> str(X_train)
'data.frame':   500 obs. of  4 variables:
 $ V2: int  1 1 1 1 1 1 1 1 1 1 ...
 $ V3: int  1 1 1 1 1 1 1 1 1 1 ...
 $ V4: int  1 1 1 1 1 2 2 2 2 2 ...
 $ V5: int  1 2 3 4 5 1 2 3 4 5 ...
> str(X_test)
'data.frame':   122 obs. of  4 variables:
 $ V2: int  5 5 5 5 5 5 5 5 5 5 ...
 $ V3: int  1 1 1 1 1 1 1 1 1 1 ...
 $ V4: int  1 1 2 2 2 2 2 3 3 3 ...
 $ V5: int  4 5 1 2 3 4 5 1 2 3 ...
> str(Y_train)
 Factor w/ 3 levels "B","L","R": 1 3 3 3 3 3 3 3 3 3 ...
rpart(formula, data, weights, subset, na.action = na.rpart, method,
  model = FALSE, x = FALSE, y = TRUE, parms, control, cost, ...) 
data_train <- head(balance_data,100)
...
tree_model <- rpart(V1 ~ V2 + V3 + V4, data_train)