dplyr::recode superseded by dplyr::case_match in upcoming version of dplyr.

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 }})

My question : will the case_match function support unquote splicing with !!! ?

Many thanks for your attention.


I don't see how it would directly, as its syntax is different, however, I'm doubtful it would be useful to add in that functionality, as recode is not being removed, only not being recommended for cases where case_match is presumed to be better.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.