I am getting very tired of writing as.numeric(as.character(my.factor))
if I want to get the numeric value of a factor in R. Although it works, it is not self-evident what the code does and it just feels plain wrong to convert numbers to character strings and back again to do anything with them. Is there a simpler and more self-explanatory way like factor.values(my.factor)
?
It has been suggested to packing it away in a custom function like
factor.values = function(x) as.numeric(levels(x))[x] # get the actual values of a factor with numeric labels
the problem with this solution is that it must be copy-pasted between scripts if it is to be reproducible by collaborators. I'm asking if there is a short built-in method to do it. I know this is a VERY small problem, but since it's a frequent one and many find the commonly presented solution anti-intuitive, I raise it anyway.
The problem
Fpr the unitiated, if you have a factor and want to do numeric operations on it you run into a number of problems:
> my.factor = factor(c(1, 1, 2, 5, 8, 13, 21))
> sum(my.factor) # let's try a numeric operation
Error in Summary.factor(1:6, na.rm = FALSE) :
sum not meaningful for factors
> as.numeric(my.factor) # oh, let's make it numeric then.
[1] 1 1 2 3 4 5 6 # argh! levels numbers and not values
> as.character(my.factor) # because the web told me so.
[1] "1" "1" "2" "5" "8" "13" "21" # closer...
> as.numeric(as.character(my.factor)) # NOT short or self-explanatory!
[1] 1 1 2 5 8 13 21 # finally we can sum ...
> sum(as.numeric(as.character(my.factor)))
[1] 51