R/factor_to_numeric.R

Defines functions factor_to_num

Documented in factor_to_num

#' Convert a Factor to Numeric
#'
#' Convert a factor with numeric levels to a non-factor (numeric).
#'
#' @param x A vector containing a factor with numeric levels.
#'
#' @return The input factor made into a numeric vector.
#'
#' @export
#'
#' @examples
#' x <- factor(c(3, 4, 9, 4, 9), levels = c(3, 4, 9))
#' factor_to_num(x)
factor_to_num <- function(x) {
  names_factor <- names(x)
  x <- as.numeric(as.character(x))
  names(x) <- names_factor
  return(x)
}

Try the nima package in your browser

Any scripts or data that you put into this service are public.

nima documentation built on March 13, 2020, 2:10 a.m.