我正在查看Titanic dataset
,并且想在Sex变量上运行One-Hot Encoding
。但是我得到一个错误,即无法将dummyVars
类转换为数据帧。
错误是:cannot coerce class ‘"dummyVars"’ to a data.frame
。
我遵循了此演练:url
# Setup
# install.packages('caret')
library(caret)
# Sample Data
head(cln_dados_treino)
Survived Pclass Sex Age SibSp Parch Fare
0 3 male 22 1 0 7.2500
1 1 female 38 1 0 71.2833
1 3 female 26 0 0 7.9250
1 1 female 35 1 0 53.1000
0 3 male 35 0 0 8.0500
0 3 male 24 0 0 8.4583
# One-Hot Encoding to Variable Sex
dv_cln_dados_treino <- dummyVars(' ~ Sex', data = cln_dados_treino)
dv_cln_dados_treino <- data.frame(predict(teste, newdata = dv_cln_dados_treino))
然后出现错误:
Error in as.data.frame.default(newdata) :
cannot coerce class ‘"dummyVars"’ to a data.frame
Survived Pclass Sex.male Sex.female Age SibSp Parch Fare
0 3 1 0 22 1 0 7.2500
1 1 0 1 38 1 0 71.2833
1 3 0 1 26 0 0 7.9250
1 1 0 1 35 1 0 53.1000
0 3 1 0 35 0 0 8.0500
0 3 1 0 24 0 0 8.4583