In the dplyr package, recode() has been superseded in favor of case_match(). Is there a way to use labels stored in, for example, char array to recode values using case_match()?
For example, with recode() I can store labels in a char array (or read them from a CSV file) and use them for recoding:
lbls <- c(
'male' = 'Man',
'female' = 'Woman'
)
starwars %>%
select( sex ) %>%
mutate(
sex = recode( sex, !!!lbls )
)
# A tibble: 87 × 1
# sex
# <chr>
# 1 Man
# 2 none
# 3 none
# 4 Man
# 5 Woman
# ...
However, since case_match() requires two-sided formulas (old_values ~ new_value), that does not work. Is there a way to use stored values also in case_match()?
You can create a set of rules to be evaluated.
tidyverseapproachAs you're using
dplyrlet's go all in:You can then turn this character vector into a list of
callobjects withrlang::parse_exprs(). Then inject the list into the function call as arguments using the splice operator,!!!:base R approach
We can also do the parsing and splicing in base R. For me it's a little clearer what's going on. We can define rules with
sprintf()instead ofglue, as suggested by Darren Tsai.To get the character vector into a list of language objects, instead of
parse_exprs()we can usestr2lang(). Then!!!is a way of applyingcase_match()to a list of arguments, i.e. the equivalent ofdo.call().A note on
.defaultNote that unlike
recode, we need to providecase_match()with the.defaultparameter:If this is not provided, any value not specified (e.g.
"none") becomesNA