> 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
Ugh. I thought the definition of as.numeric.result (default TRUE) was that if the results are all numerals, they would be coerced to numeric.
How do I get the results of this recoding to be numeric?
If you look carefully at the documentation on
recode
you'll see this:So you need to specify
as.factor.result=FALSE
I think:edit Since the default of
as.numeric.result
is TRUE, you only need to specifyas.factor.result=FALSE
, rather than specifying both of them.Try using
as.numeric
againFrom
?recode
you should note what is said about theas.numeric.result
argument:as.factor.result
defaults toTRUE
so the result will always be a factor, regardless of what you setas.numeric.result
to. To get the desired behaviour, set bothas.factor.result = FALSE
andas.numeric.result = TRUE
: