This function allows you to use multiple if/else statements in one call.

It is called like dplyr::case_when(), but utilizes data.table::fifelse() in the background for improved performance.

case_when(..., .default = NA, .ptype = NULL, .size = NULL)

Arguments

...

A sequence of two-sided formulas. The left hand side gives the conditions, the right hand side gives the values.

.default

The default value if all conditions evaluate to FALSE.

.ptype

Optional ptype to specify the output type.

.size

Optional size to specify the output size.

Examples

df <- tidytable(x = 1:10)

df %>%
  mutate(case_x = case_when(x < 5 ~ 1,
                            x < 7 ~ 2,
                            TRUE ~ 3))
#> # A tidytable: 10 × 2
#>        x case_x
#>    <int>  <dbl>
#>  1     1      1
#>  2     2      1
#>  3     3      1
#>  4     4      1
#>  5     5      2
#>  6     6      2
#>  7     7      3
#>  8     8      3
#>  9     9      3
#> 10    10      3