View source: R/as_numeric_factor.R
as_numeric_factor | R Documentation |
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))
.
as_numeric_factor(x)
x |
an object, for example a factor. |
A numeric object
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
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)))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.