rdplyrrecode

how to pass a named vector or two vectors as arguments to dplyr::recode


I'd like to pass either a named vector or two vectors to dplyr::recode. Let's say I've got:

library(dplyr)
set.seed(1)
x <- c("customer", sample(c("a", "b", "c"), 10, replace = TRUE))
recode_tbl <- tibble(letter = letters[1:3], fruit = c("apple", "banana", "cranberry"))

What I'd like to do is use the columns of recode_tbl to recode x, without having to specify the pairs manually as:

recode(x, a = "apple", b = "banana", c = "cranberry")

Something like:

recode(x, as.name(recode_tbl$letter) = recode_tbl$fruit)

That obviously doesn't work. I'm not averse to trying NSE but if someone could get the ball rolling that would be great.

Thanks.


Solution

  • We can do this in base R

    x1 <- unname(setNames(recode_tbl$fruit, recode_tbl$letter)[x])
    x1[is.na(x1)] <- x[is.na(x1)]
    

    Or use do.call with recode

    do.call(dplyr::recode, c(list(x), setNames(recode_tbl$fruit, recode_tbl$letter)))
    #[1] "customer"  "apple"     "banana"    "banana"    "cranberry" "apple"
    #[8] "cranberry" "cranberry" "banana"    "banana"    "apple"   
    

    Or when passing a named vector to rename levels use !!! to splice

    recode(x, !!!setNames(recode_tbl$fruit, recode_tbl$letter))