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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/utility_functions.R
\name{fac2num}
\alias{fac2num}
\title{Convert factor variable to numeric}
\source{
\href{https://stackoverflow.com/questions/3418128/how-to-convert-a-factor-to-an-integer-numeric-without-a-loss-of-information}{Stackoverflow thread}
}
\usage{
fac2num(x)
}
\arguments{
\item{x}{a factor variable with numbers as levels}
}
\value{
A numeric vector based on the levels of \code{x}.
}
\description{
Convert factor variable with numbers as levels into a numeric variable
}
\details{
For example, a factor with levels \code{c("5","7")} is converted into
a numeric variable with values \code{c(5,7)}.
}
\examples{
## 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)
)
}
\seealso{
\verb{[robust_values]}
}
|