Convert variable to integer avoiding potential issues with factors

as_integer(x)

Arguments

x

Input variable

Value

Integer

Examples

as_integer(rnorm(10))
#> [1] -1 0 -2 0 0 1 -1 0 0 0
as_integer(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_integer(as.factor(5:10))
#> [1] 5 6 7 8 9 10
#> [1] 1 2 3 4 5 6
as_integer(c("a","b"))
#> [1] 1 2
as_integer(c("0","1"))
#> [1] 0 1
as_integer(as.factor(c("0","1")))
#> [1] 0 1