fct_case_when: Obtain a factor after a 'case_when'

View source: R/fct_case_when.R

fct_case_whenR Documentation

Obtain a factor after a case_when

Description

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.

Usage

fct_case_when(...)

Arguments

...

Input of a case_when.

Details

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.

Value

A factor vector.

Examples


# 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' )
     )


octmedina/ksnet documentation built on April 18, 2023, 3:34 p.m.