Skip to contents

A convenience wrapper around dplyr::case_match(), with .default set to the input parameter .x (i.e. keeping values unaffected by any switch statements as they are)

Usage

case_modify(.x, ...)

Arguments

.x

A vector to match against.

...

<dynamic-dots> A sequence of two-sided formulas: old_values ~ new_value. The right hand side (RHS) determines the output value for all values of .x that match the left hand side (LHS).

The LHS must evaluate to the same type of vector as .x. It can be any length, allowing you to map multiple .x values to the same RHS value. If a value is repeated in the LHS, i.e. a value in .x matches to multiple cases, the first match is used.

The RHS inputs will be coerced to their common type. Each RHS input will be recycled to the size of .x.

Value

A vector with the same size as .x.

Examples

dummy_df <- data.frame("a" = c(1:3))
# only replace 3 by 4, keep the rest
dplyr::mutate(dummy_df, a = case_modify(a, 3 ~ 4))
#>   a
#> 1 1
#> 2 2
#> 3 4