【发布时间】:2021-10-06 09:48:45
【问题描述】:
我正在尝试将字符串变量(例如“没有时间”、“有时”、“经常”...)中的多列数据重新编码为数值(例如“没有时间”= 0)。我已经看到许多对类似问题的不同回复,但是当我尝试这些时,他们似乎删除了所有数据并将其替换为 NA。
For_Analysis <- data.frame(Q11_1=c("None of the time", "Often", "Sometimes"),
Q11_2=c("Sometimes", "Often", "Never"), Q11_3=c("Never", "Never", "Often"))
For_Analysis <- For_Analysis%>%
mutate_at(c("Q11_1", "Q11_2", "Q11_3"),
funs(recode(., "None of the time"=1, "Rarely"=2,
"Some of the time"=3, "Often"=4, "All of the time"=5)))
当我运行第二段代码时,我得到以下输出
## There were 14 warnings (use warnings() to see them)
并且数据帧中的所有数据都被重新编码为NA,而不是我想要的数值。
【问题讨论】:
-
你在找
FA <- ifelse(test = "None of the time", yes = 1, no = ifelse(test = "Rarely", 2, no = ifelse(...)))