问题描述
我有一个数据帧(MyData1),其中因子变量分为多个级别:“同意”,“非常同意”,“不同意”等。我试图将级别组合为二进制(例如,如果同意/反对,则为“是”)。 / p>
我一直沿用这些思路:
combineLevels(MyData1$V45,levs=c("disagree"),newLabel="False")
>Error: requested levels: " disagree " are not in the legal list of factor levels:" Agree disagree Neutral Strongly agree
重新编码功能出现相同的问题。有没有办法弄清楚为什么R无法识别能够产生功能的水平?
解决方法
您没有指出函数combineLevels
的来源(至少有两个版本)。在基数R中很简单:
set.seed(42)
V45 <- sample(c("Strongly Agree","Agree","Disagree","Strongly Disagree"),100,replace=TRUE)
V45 <- factor(V45,levels=c("Strongly Agree","Strongly Disagree"))
table(V45)
# V45
# Strongly Agree Agree Disagree Strongly Disagree
# 28 30 16 26
只需更改级别:
V45binary <- V45 # So we can recover the original if something goes wrong
levels(V45binary) <- c("Yes","Yes","False","False")
table(V45binary)
# V45binary
# Yes False
# 58 42