as_numeric_factor: Convert a factor to integer/numeric without loss of...

View source: R/as_numeric_factor.R

as_numeric_factorR Documentation

Convert a factor to integer/numeric without loss of information

Description

To transform a factor f to approximately its original numeric values, as.numeric(levels(f))[f] is recommended and slightly more efficient than as.numeric(as.character(f)).

Usage

as_numeric_factor(x)

Arguments

x

an object, for example a factor.

Value

A numeric object

References

https://stackoverflow.com/questions/3418128/how-to-convert-a-factor-to-integer-numeric-without-loss-of-information https://cran.r-project.org/doc/FAQ/R-FAQ.html#How-do-I-convert-factors-to-numeric_003f

Examples

library(dplyr)
library(tibble)

df <- tibble::tibble(
  foo = factor(sample(x = c(4, 5, 6),  size = 50, replace = TRUE),
               levels = c(6, 5, 4))
)

df

df %>%
  mutate(foo_base = as.numeric(foo),
         foo_lamisc = as_numeric_factor(foo),
         foo_alt = as.numeric(as.character(foo)))

emilelatour/lamisc documentation built on April 9, 2024, 10:33 a.m.