Finally, #dplyr functionality I've been hacking through small functions for about a decade and a half: https://dplyr.tidyverse.org/reference/recode-and-replace-values.html

Recode and replace values — recode-and-replace-values
recode_values() and replace_values() provide two ways to map old values to new values. They work by matching values against x and using the first match to determine the corresponding value in the output vector. You can also think of these functions as a way to use a lookup table to recode a vector. Use recode_values() when creating an entirely new vector. Use replace_values() when partially updating an existing vector. If you are just replacing a few values within an existing vector, then replace_values() is always a better choice because it is type stable and better expresses intent. A major difference between the two functions is what happens when no cases match: recode_values() falls through to a default. replace_values() retains the original values from x. These functions have two mutually exclusive ways to use them: A formula-based approach, i.e. recode_values(x, from1 ~ to1, from2 ~ to2), similar to case_when(), which is useful when you have a small number of cases. A vector-based approach, i.e. recode_values(x, from = from, to = to), which is useful when you have a pre-built lookup table (which may come from an external source, like a CSV file). See vignette("recoding-replacing") for more examples.





