Convert variable to numeric avoiding potential issues with factors

as_numeric(x)

Arguments

x

Input variable

Value

Numeric

Examples

as_numeric(rnorm(10))
#> [1] -0.55369938 0.62898204 2.06502490 -1.63098940 0.51242695 -1.86301149 #> [7] -0.52201251 -0.05260191 0.54299634 -0.91407483
as_numeric(letters)
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #> [26] 26
as_numeric(as.factor(5:10))
#> [1] 5 6 7 8 9 10
#> [1] 1 2 3 4 5 6
as_numeric(c("a","b"))
#> [1] 1 2
as_numeric(c("3","4"))
#> [1] 3 4
as_numeric(as.factor(c("3","4")))
#> [1] 3 4