|
### CONVERT THE FACTOR TO DUMMIES ###
library(caret)
dummies <- predict(dummyVars(~ Species, data = iris), newdata = iris)
head(dummies, n = 3)
### CONVERT DUMMIES TO THE FACTOR ###
header <- unlist(strsplit(colnames(dummies), '[.]'))[2 * (1:ncol(dummies))]
species <- factor(dummies %*% 1:ncol(dummies), labels = header)
str(species)
#顺便提示一下,rda或cca函数,如果用公式模型(就是表达式带有~)已经自动将分类变量变为哑变量(0-1数据)
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-11-24 19:53
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社