如何将变量重新编码为数字?

时间:2011-07-14 22:32:36

标签: r r-car

> 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

唉。我认为definition of as.numeric.result(默认为TRUE)是如果结果都是数字,它们将被强制为数字。

如何将此重新编码的结果设为数字?

3 个答案:

答案 0 :(得分:5)

如果仔细查看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)

修改的 由于as.numeric.result的默认值为TRUE,因此您只需指定as.factor.result=FALSE,而不是同时指定它们。

答案 1 :(得分:3)

再次尝试使用as.numeric

> bar <- as.numeric(foo)
> bar
[1] 1 2 3 1
> str(bar)
 num [1:4] 1 2 3 1

答案 2 :(得分:3)

?recode开始,你应该注意关于as.numeric.result论证的内容:

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默认为TRUE,因此无论您将as.numeric.result设置为什么,结果始终都是一个因素。要获得所需的行为,请同时设置as.factor.result = FALSE as.numeric.result = TRUE

> recode(df$value, "'A'=1; 'B'=2; 'C'=3;", as.numeric.result=TRUE, 
         as.factor.result = FALSE)
[1] 1 2 3 1