View source: R/fct_case_when.R
fct_case_when | R Documentation |
case_when
The output of the case_when
function is a character, which means that when
you want to work with factors you have to apply the factor
function afterwards.
fct_case_when(...)
... |
Input of a |
With this function the usual case_when
procedure is followed but a factor is
obtained with the levels ordered according to the appearance in
the case_when
input.
A factor vector.
# Comparing the outputs of the two functions:
library(dplyr)
as_tibble(mtcars) %>%
select( cyl ) %>%
mutate(
cyl_case_when = case_when(
cyl == 4 ~ 'Bajo',
cyl == 6 ~ 'Medio',
cyl == 8 ~ 'Alto' ),
cyl_fct_case_when = fct_case_when(
cyl == 4 ~ 'Bajo',
cyl == 6 ~ 'Medio',
cyl == 8 ~ 'Alto' )
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.