0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Error in eval(expr, envir, encols) : object X not found

0
Last updated at Posted at 2017-03-17

概要

randomForest()使用時に、
「Error in eval(expr, envir, encols) : object X not found」
エラーがでた

使用したデータ

df とする

ID  aim_col   '"MAY"'   JUNE  JULY
A   1   0.11   0.2    0.11
C   1   0.2   0.5    0.22
B   0   0.2   0.113    0.6
D   1   0.3   0.11   0.11

実行内容と結果

train_model <- randomForest(aim_col , #目的変数
                            data = df[,c(2:ncol(df)] #使用するデータ
                            #細かいオプションは省略
)

を実行すると

Error in eval(expr, envir, encols) : object '"MAY"' not found

というようなエラーがでた。

'"MAY"'は、データ中の説明変数としているカラムのひとつだが、
どうやらこの名前がよくないっぽい。前後に'とか入っちゃってるし。

解決策

# colnames()でカラム名の変更
# ID,aim_col のカラム名はそのままにして、
# その他のカラムは、col_1,col_2...となるようにする
colnames(df) <-c("ID", "aim_col", paste0("col_", seq(1:ncol(df)-2 ) ) )

ここでのポイントは、ID,aim_col以外のカラム名を「1,2,,,」
のようにしてしまうと動かないことが多いので、「col_1,col_2,,,」としていること。

変換後データ

ID  aim_col   col_1   col_2  col_3
A   1   0.11   0.2    0.11
C   1   0.2   0.5    0.22
B   0   0.2   0.113    0.6
D   1   0.3   0.11   0.11

実行できるようになったはず。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?