Please Consider Using wrapr::let() for Replacement Tasks

June 26, 2017
By

(This article was first published on R – Win-Vector Blog, and kindly contributed to R-bloggers)

From dplyr issue 2916.

The following appears to work.

suppressPackageStartupMessages(library("dplyr"))

COL <- "homeworld"
starwars %>%
  group_by(.data[[COL]]) %>%
  head(n=1)
## # A tibble: 1 x 14
## # Groups:   COL [1]
##             name height  mass hair_color skin_color eye_color birth_year
##                                      
## 1 Luke Skywalker    172    77      blond       fair      blue         19
## # ... with 7 more variables: gender , homeworld , species ,
## #   films , vehicles , starships , COL 

Though notice it reports the grouping is by "COL", not by "homeworld". Also the data set now has 14 columns, not the original 13 from the starwars data set.

And this seemingly similar variation (currently) throws an exception:

homeworld <- "homeworld"

starwars %>%
  group_by(.data[[homeworld]]) %>% 
  head(n=1) 
## Error in mutate_impl(.data, dots): Evaluation error: Must subset with a string.

I know this will cost me what little community good-will I might have left (after already having raised this, unsolicited, many times), but please consider using our package wrapr::let() for tasks such as the above.

library("wrapr")

let(
  c(COL = "homeworld"),
  
  starwars %>%
    group_by(COL) %>%
    head(n=1)
)
## # A tibble: 1 x 13
## # Groups:   homeworld [1]
##             name height  mass hair_color skin_color eye_color birth_year
##                                      
## 1 Luke Skywalker    172    77      blond       fair      blue         19
## # ... with 6 more variables: gender , homeworld , species ,
## #   films , vehicles , starships 
let(
  c(homeworld = "homeworld"),
  
  starwars %>%
    group_by(homeworld) %>% 
    head(n=1)
)
## # A tibble: 1 x 13
## # Groups:   homeworld [1]
##             name height  mass hair_color skin_color eye_color birth_year
##                                      
## 1 Luke Skywalker    172    77      blond       fair      blue         19
## # ... with 6 more variables: gender , homeworld , species ,
## #   films , vehicles , starships 

Some explanation can be found here.

To leave a comment for the author, please follow the link and comment on their blog: R – Win-Vector Blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)