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
Suggestion: recode to accept named list #2505
Comments
Part of #2477 |
@hadley Has this been implemented?
|
Here is my left_join solution
x <- c("a", "b", "c")
MAP <- list(a = "1", b = "2", c = "3")
dplyr::recode(x, MAP)
#> Error: Argument 2 must be named, not unnamed
x_lj <- tibble::tibble(x = x)
MAP_lj <- dplyr::bind_cols(x = names(MAP), y = as.character(MAP))
dplyr::left_join(x_lj, MAP_lj, by = "x")
#> # A tibble: 3 x 2
#> x y
#> <chr> <chr>
#> 1 a 1
#> 2 b 2
#> 3 c 3 |
@cg1122 I figured it out via http://dplyr.tidyverse.org/articles/programming.html: x <- c("a", "b", "c")
MAP <- list(a = "1", b = "2", c = "3")
dplyr::recode(x, !!!MAP)
#> [1] "1" "2" "3" |
Rather than hardcoding via case_when. See tidyverse/dplyr#2505 (comment) for understanding the !!! notation
I was about to suggest a |
It would be really convenient if recode could accept a named list as an argument instead of having to specify each of the input parameters.
Currently to use the function you have to do something like
I propose that the following be valid
This allows for standardized maps to be setup and used across a project which is useful when you are dealing with short variable names or coded parameter terms which need to be transformed into more wordy labels later on.
The text was updated successfully, but these errors were encountered: