tidyverse / dplyr

dplyr: A grammar of data manipulation
https://dplyr.tidyverse.org/
Other
4.75k stars 2.12k forks source link

plyr mapvalues function ported into dplyr #4310

Closed anuj2054 closed 5 years ago

anuj2054 commented 5 years ago

Wondering why the mapvalues function in plyr has not been ported into dplyr ?

If it has, what is its analog in dplyr ?

cderv commented 5 years ago

I believe you are looking for dplyr::recode. Examples from mapvalues help page:

x <- c("a", "b", "c")

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

y <- factor(c("a", "b", "c", "a"))
plyr::mapvalues(y, c("a", "c"), c("A", "C"))
#> [1] A b C A
#> Levels: A b C
dplyr::recode(y, a = "A", c = "C")
#> [1] A b C A
#> Levels: A b C

z <- c(1, 4, 5, 9)
plyr::mapvalues(z, from = c(1, 5, 9), to = c(10, 50, 90))
#> [1] 10  4 50 90
dplyr::recode(z, `1`  = 10, `5` = 50, `9` = 90)
#> [1] 10  4 50 90

# using vectors with tidyeval and splicing
library(dplyr)
from <- c(1, 5, 9)
to <- c(10, 50, 90)
recode(z, !!! setNames(to, from))
#> [1] 10  4 50 90

Created on 2019-04-01 by the reprex package (v0.2.1.9000)

romainfrancois commented 5 years ago

Thanks @cderv. @anuj2054 please consider https://community.rstudio.com for questions.

lock[bot] commented 4 years ago

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/