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

Suggestion: recode to accept named list #2505

Closed
gowerc opened this issue Mar 7, 2017 · 5 comments
Closed

Suggestion: recode to accept named list #2505

gowerc opened this issue Mar 7, 2017 · 5 comments

Comments

@gowerc
Copy link

gowerc commented Mar 7, 2017

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

x <- c( "a" , "b" , "c")
recode( x , a = "1" , b = "2" , c = "3") 

I propose that the following be valid

MAP <- list(
	a = "1" ,
	b = "2" ,
	c = "3"
)

recode( x , MAP )

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.

@hadley
Copy link
Member

hadley commented Mar 7, 2017

Part of #2477

@hadley hadley closed this as completed Mar 7, 2017
@ahcyip
Copy link
Contributor

ahcyip commented Jun 19, 2017

@hadley Has this been implemented?
I've been trying to use named lists in dplyr::recode() but it doesn't seem to work.
I suppose I'll stick with the tried and true left_join lookup table for now.

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

@ahcyip
Copy link
Contributor

ahcyip commented Jun 19, 2017

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

@ahcyip
Copy link
Contributor

ahcyip commented Jun 19, 2017

@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"

Thanks @hadley @lionel- and tidyverse !

peterdesmet added a commit to trias-project/alien-plants-belgium that referenced this issue Jun 28, 2017
Rather than hardcoding via case_when.

See tidyverse/dplyr#2505 (comment) for understanding the !!! notation
@billdenney
Copy link
Contributor

I was about to suggest a recode_ function that does what @ahcyip suggested. Can that example or one largely similar be added to the documentation, please?

@lock lock bot locked as resolved and limited conversation to collaborators Jun 7, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants