Dear all,
dplyr::recode will be superseded by dplyr::case_match in the upcoming production version of dplyr.
The snippet below is taken from the dplyr vignette: Recode values — recode • dplyr
# Use a named character vector for unquote splicing with !!!
level_key <- c(a = "apple", b = "banana", c = "carrot")
recode(char_vec, !!!level_key)
#> [1] "banana" "carrot" "apple" "banana" "carrot" "banana" "banana"
#> [8] "carrot" "carrot" "carrot"
I use the recode + unquote splicing with !!! extensively in my code. That way, I can put the mapping vector on top of the code.
Being able to unquote splicing also enables me to write functions like this:
recode_drop_na <- function(df, col, mapping_vars) {
df <- df |>
mutate("{{ col }}" := recode({{ col }}, !!!mapping_vars, .default = NA_character_)) |>
drop_na({{ col }})
df
}
My question : will the case_match function support unquote splicing with !!! ?
Many thanks for your attention.
Frédéric.