Converting factors to numeric
as_numeric(x) # S3 method for default as_numeric(x) # S3 method for factor as_numeric(x)
x | a vector |
---|
a numeric vector of the same length as x
in which each value
of x
is converted to its actual numeric value
as_numeric()
is motivated by the need to convert factors to their
numerical representation, rather than their numerically encoded levels. See
the examples for differences between base::as.numeric()
and
as_numeric
.
as_numeric.factor
directly follows the suggestion from the
Warning
section of factor
.
x <- factor(c(-1.9, 0.2, 3.5)) as.numeric(x)#> [1] 1 2 3as_numeric(x)#> [1] -1.9 0.2 3.5