1

我想将我的数据从字符重新编码为数字。这是我失败的尝试:

library(car) # needed for recode
survey1$V16a = recode(survey1$V16, "'Very important'=1; 'Fairly important'=2; 'Not very important'=3; 'Not important at all'=4; 'Can't choose'=99;", as.numeric.result=TRUE)

这是我随后收到的错误消息

重新编码错误(survey1$V16, "'非常重要'=1; '相当重要'=2; '不是很重要'=3; '一点也不重要'=4; '不能选择'=99;" , :

在重新编码术语中:'无法选择'= 99消息:解析错误(文本= strsplit(术语,“=”)[[1]] [1])::1:7:意外符号1:“可以”吨

4

1 回答 1

0

没有必要为此使用额外的包car。在我看来,这也降低了代码的可读性。只需使用ifelse.

survey1$V16a <- ifelse(survey1$V16a == "Very important", 1, 
                ifelse(survey1$V16a == "Fairly important", 2,
                ifelse(survey1$V16a == "Not very important", 3,
                .... )))))
于 2016-03-15T20:18:24.220 回答