case_when

0th

Percentile

A general vectorised if

This function allows you to vectorise multiple if and else if statements. It is an R equivalent of the SQL CASE WHEN statement.

Usage
case_when(...)
Arguments
...

A sequence of two-sided formulas. The left hand side (LHS) determines which values match this case. The right hand side (RHS) provides the replacement value.

The LHS must evaluate to a logical vector. The RHS does need to be logical, but all RHSs must evaluate to the same type of vector.

Both LHS and RHS may have the same length of either 1 or n. The value of n must be consistent across all cases. The case of n == 0 is treated as a variant of n != 1.

These dots support tidy dots features.

Value

A vector of length 1 or n, matching the length of the logical input or output vectors, with the type (and attributes) of the first RHS. Inconsistent lengths or types will generate an error.

Aliases
  • case_when
Examples
# NOT RUN {
x <- 1:50
case_when(
  x %% 35 == 0 ~ "fizz buzz",
  x %% 5 == 0 ~ "fizz",
  x %% 7 == 0 ~ "buzz",
  TRUE ~ as.character(x)
)

# Like an if statement, the arguments are evaluated in order, so you must
# proceed from the most specific to the most general. This won't work:
case_when(
  TRUE ~ as.character(x),
  x %%  5 == 0 ~ "fizz",
  x %%  7 == 0 ~ "buzz",
  x %% 35 == 0 ~ "fizz buzz"
)

# All RHS values need to be of the same type. Inconsistent types will throw an error.
# This applies also to NA values used in RHS: NA is logical, use
# typed values like NA_real_, NA_complex, NA_character_, NA_integer_ as appropriate.
case_when(
  x %% 35 == 0 ~ NA_character_,
  x %% 5 == 0 ~ "fizz",
  x %% 7 == 0 ~ "buzz",
  TRUE ~ as.character(x)
)
case_when(
  x %% 35 == 0 ~ 35,
  x %% 5 == 0 ~ 5,
  x %% 7 == 0 ~ 7,
  TRUE ~ NA_real_
)
# This throws an error as NA is logical not numeric
# }
# NOT RUN {
case_when(
  x %% 35 == 0 ~ 35,
  x %% 5 == 0 ~ 5,
  x %% 7 == 0 ~ 7,
  TRUE ~ NA
)
# }
# NOT RUN {
# case_when is particularly useful inside mutate when you want to
# create a new variable that relies on a complex combination of existing
# variables
starwars %>%
  select(name:mass, gender, species) %>%
  mutate(
    type = case_when(
      height > 200 | mass > 200 ~ "large",
      species == "Droid"        ~ "robot",
      TRUE                      ~  "other"
    )
  )

# Dots support splicing:
patterns <- list(
  x %% 35 == 0 ~ "fizz buzz",
  x %% 5 == 0 ~ "fizz",
  x %% 7 == 0 ~ "buzz",
  TRUE ~ as.character(x)
)
case_when(!!!patterns)
# }
Documentation reproduced from package dplyr, version 0.7.8, License: MIT + file LICENSE

Community examples

alexey.shiklomanov@gmail.com at Oct 19, 2017 dplyr v0.7.3

# Complex example ```r iris <- tibble::as_tibble(iris) ``` ## Direct entry ```r cases_uq <- list( !! Sepal.Length > 5.5 ~ "long", !! Sepal.Length > 5 ~ "medium", TRUE ~ "short" ) iris2 <- dplyr::mutate(iris, sepal_category = dplyr::case_when(!!! cases_uq)) dplyr::summarize( dplyr::group_by(iris2, sepal_category), mean = mean(Sepal.Length) ) # # A tibble: 3 x 2 #sepal_category mean #<chr> <dbl> # 1 long 6.378022 # 2 medium 5.292593 # 3 short 4.787500 ``` ## Variable as string ```r my_column <- "Sepal.Length" cases_q <- list( !! get(my_column) > 5.5 ~ "long", !! get(my_column) > 5 ~ "medium", TRUE ~ "short" ) iris2 <- dplyr::mutate(iris, sepal_category = dplyr::case_when(!!! cases_q)) dplyr::summarize( dplyr::group_by(iris2, sepal_category), mean = mean(Sepal.Length) ) # # A tibble: 3 x 2 #sepal_category mean #<chr> <dbl> # 1 long 6.378022 # 2 medium 5.292593 # 3 short 4.787500 ``` ## Function ```r summary_function <- function(my_column) { cases <- list( !! get(my_column) > 5.5 ~ "long", !! get(my_column) > 5 ~ "medium", TRUE ~ "short" ) iris2 <- dplyr::mutate(iris, group = dplyr::case_when(!!! cases)) dplyr::summarize( dplyr::group_by(iris2, group), mean = mean(!! rlang::sym(my_column)) ) } summary_function("Sepal.Length") # # A tibble: 3 x 2 # group mean # <chr> <dbl> # 1 long 6.378022 # 2 medium 5.292593 # 3 short 4.787500 summary_function("Petal.Length") # # A tibble: 3 x 2 # group mean # <chr> <dbl> # 1 long 5.992000 # 2 medium 5.241176 # 3 short 3.007407 ```