View source: R/utility_functions.R
fac2num | R Documentation |
Convert factor variable with numbers as levels into a numeric variable
fac2num(x)
x |
a factor variable with numbers as levels |
For example, a factor with levels c("5","7")
is converted into
a numeric variable with values c(5,7)
.
A numeric vector based on the levels of x
.
[robust_values]
## this is often not intended
as.numeric(factor(c(5,7))) ## result: c(1,2)
## but this
fac2num(factor(c(5,7))) ## result: c(5,7)
## however
as.numeric(factor(c("5","7","a"))) ## 1:3
suppressWarnings(
fac2num(factor(c("5","7","a"))) ## c(5,7,NA)
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.