Converting factors to numeric

as_numeric(x)

# S3 method for default
as_numeric(x)

# S3 method for factor
as_numeric(x)

Arguments

x

a vector

Value

a numeric vector of the same length as x in which each value of x is converted to its actual numeric value

Details

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.

See also

as_numeric.factor directly follows the suggestion from the Warning section of factor.

Examples

x <- factor(c(-1.9, 0.2, 3.5)) as.numeric(x)
#> [1] 1 2 3
as_numeric(x)
#> [1] -1.9 0.2 3.5