Robert's Data Science Blog

mapvalues with dplyr

The plyr package has the neat function mapvalues for changing values in a vector. An example from plyr’s documentation:

x <- c("a", "b", "c")
plyr::mapvalues(x, c("a", "c"), c("A", "C"))
## [1] "A" "b" "C"

I prefer the dplyr package over plyr and I would like to avoid using plyr for a single function. A dplyr counterpart to mapvalues is recode:

dplyr::recode(x, a = "A", c = "C")
## [1] "A" "b" "C"

This seems reasonable for a small number of recodings, but the mapvalues approach does have an appeal when a large number values should be recoded. Can we do something that is (almost) as easy with dplyr?

My actual usecase is a vector with values being the month number that should be shifted. With mapvalues:

plyr::mapvalues(1:12, 1:12, c(7:12, 1:6), warn_missing = FALSE)
##  [1]  7  8  9 10 11 12  1  2  3  4  5  6

With recode we can create a named vector with the recoding

recoding <- c(7:12, 1:6)
names(recoding) <- 1:12
recoding
##  1  2  3  4  5  6  7  8  9 10 11 12 
##  7  8  9 10 11 12  1  2  3  4  5  6

and then splice this in recode:

dplyr::recode(1:12, !!!recoding)
##  [1]  7  8  9 10 11 12  1  2  3  4  5  6