> library(car)
> df = data.frame(value=c('A', 'B', 'C', 'A'))
> foo = recode(df$value, "'A'=1; 'B'=2; 'C'=3;", as.numeric.result=TRUE)
> mean(foo)
[1] NA
Warning message:
In mean.default(foo) : argument is not numeric or logical: returning NA
> foo
[1] 1 2 3 1
Levels: 1 2 3
啊。我以为as.numeric.result 的定义 http://hosho.ees.hokudai.ac.jp/~kubo/Rdoc/library/car/html/recode.html(默认 TRUE)是如果结果都是数字,它们将被强制为数字。
如何将重新编码的结果转换为数字?
如果您仔细查看有关的文档recode
你会看到这个:
as.factor.result return a factor; default is TRUE if var is a factor, FALSE otherwise.
as.numeric.result if TRUE (the default), and as.factor.result is FALSE,
then the result will be coerced to numeric if all values in the
result are numerals—i.e., represent numbers.
所以你需要指定as.factor.result=FALSE
我认为:
foo = recode(df$value, "'A'=1; 'B'=2; 'C'=3;", as.factor.result=FALSE)
edit由于默认为as.numeric.result
为 TRUE,您只需指定as.factor.result=FALSE
,而不是指定它们两者。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)