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)
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.xthat 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.xvalues to the same RHS value. If a value is repeated in the LHS, i.e. a value in.xmatches 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.
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