% 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 `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{
\code{\link{robust_values}}
}
