Convert a factor with numeric labels into a numeric vector
Source:R/shorthand.R
factor_conversion.RdConverting a factor with as.numeric() returns the underlying integer
codes, not the labels, which is rarely what you want when the labels are
themselves numbers. f.as.numeric() returns the labels as numbers
instead.
Examples
x <- factor(c(11, 22, 33, 99))
as.numeric(x)
#> [1] 1 2 3 4
# 1 2 3 4 # the integer codes - NOT usually what you want
f.as.numeric(x)
#> [1] 11 22 33 99
# 11 22 33 99 # the labels as numbers - usually what you want
# equivalent to the clunkier base idiom:
as.numeric(as.character(x))
#> [1] 11 22 33 99