Learn R Programming

popEpi (version 0.2.1)

fac2num: Convert factor variable to numeric

Description

Convert factor variable with numbers as levels into a numeric variable

Usage

fac2num(x)

Arguments

x
a factor variable with numbers as levels

source

http://stackoverflow.com/questions/3418128/how-to-convert-a-factor-to-an-integer-numeric-without-a-loss-of-information{Stackoverflow thread}

Details

For example, a factor with levels c("5","7") is converted into a numeric variable with values c(5,7).

See Also

robust_values

Examples

Run this code
## 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

fac2num(factor(c("5","7","a"))) ## result: c(5,7,NA) with warning

Run the code above in your browser using DataLab