# 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)
# }
Run the code above in your browser using DataLab