R/factor2numeric.R

Defines functions factor2numeric

Documented in factor2numeric

##' A simple function for converting factors to numeric values
##'
##' @export
##' @param x A vector of type \code{factor}
##'
##' @return \code{x} converted to a numeric vector
##'
##' @examples
##' # Define a factor object
##' y <- factor(5:7)
##' y
##'
##' # incorrectly convert to numeric
##' as.numeric(y)
##'
##' # correctly convert
##' factor2numeric(y)
##'

factor2numeric <- function(x) {

  stopifnot(is.factor(x))

  return(as.numeric(levels(x))[x])

} # factor2numeric
pnnl/Smisc documentation built on Oct. 18, 2020, 6:18 p.m.