Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port plyr::mapvalues() equivalent into dplyr #5919

Closed
aloboa opened this issue Jun 18, 2021 · 6 comments
Closed

Port plyr::mapvalues() equivalent into dplyr #5919

aloboa opened this issue Jun 18, 2021 · 6 comments

Comments

@aloboa
Copy link

aloboa commented Jun 18, 2021

ref. #4310
(cannot write into that issue because it was blocked).

The currently suggested alternatives for plyr::mapvalues() in dplyr are inconvenient.
In the case of

dplyr::recode(z, `1`  = 10, `5` = 50, `9` = 90)

the solution is unbearable as soon as the "from" and "to" vectors are longer than 3 elements.

In the case of

recode(z, !!! setNames(to, from))

the code is just inextricable.

It would be so much friendlier just having a function dplyr::mapvalues()
as a wrapper for the last solution, i.e. as suggested in
https://stackoverflow.com/questions/62216940/dplyr-alternative-for-plyrmapvalues-recode-using-dictionary

While implementing that solution as a personal function is easy, using personal functions for
such a general tool is not a good idea, as R scripts would easily become unintelligible to other people.

@romainfrancois
Copy link
Member

We are in the process of designing a better recode() in the funs package. We're not sure at this point what it will be.

@aloboa
Copy link
Author

aloboa commented Nov 5, 2022

I'm confused, do we have an equivalent to plyr::mapvalues() in dplyr or in any other tidyverse package?
If you have closed this, I understand it should exist, but cannot find it.

@hadley
Copy link
Member

hadley commented Nov 5, 2022

The new case_switch() is a good equivalent.

@thomasjwood
Copy link

thomasjwood commented Feb 1, 2024

Is case_switch() documented somewhere?

@olivroy
Copy link

olivroy commented Feb 13, 2024

Probably referring to case_match().

@aloboa
Copy link
Author

aloboa commented Feb 14, 2024

Could you include as an example the equivalent to:

z <- c(1, 4, 5, 9)
dict <- list(from=c(1,5,9), to=c("a","x","r"))
z.new <- mapvalues(z, from = dict$from to = dict$to)

which is what is really needed by users?
Please note that being able to include an object such as dict within case_match() is much needed.
Also, I would prefer a devoted, standard "recode()" function for this specific, very usual, application of case_match()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants