case and val_match are cheaper alternatives to dplyr::case_when and
dplyr::case_match respectively.
case(..., .default = NULL)val_match(.x, ..., .default = NULL)
A vector the same length as .x or same length as the first condition in the
case of case, unless the condition length is smaller than the
rhs, in which case the length of the rhs is used.
Logical expressions or scalar values in the case of val_match.
Catch-all value or vector.
Vector used to switch values.
val_match() is a very efficient special case of the
case() function when all lhs expressions are scalars,
i.e. length-1 vectors. RHS expressions can be vectors the
same length as .x.
The below 2 expressions are equivalent.
val_match(
x,
1 ~ "one",
2 ~ "two",
.default = "Unknown"
)
case(
x == 1 ~ "one",
x == 2 ~ "two",
.default = "Unknown"
)
cheapr_if_else