Search code examples
rdplyrsummarizerlangquosure

Pass column names as strings to group_by and summarize


With dplyr starting version 0.7 the methods ending with underscore such as summarize_ group_by_ are deprecated since we are supposed to use quosures.

See: https://cran.r-project.org/web/packages/dplyr/vignettes/programming.html

I am trying to implement the following example using quo and !!

Working example:

df <- data.frame(x = c("a","a","a","b","b","b"), y=c(1,1,2,2,3,3), z = 1:6)

lFG <- df %>% 
   group_by( x,y) 
lFG %>% summarize( min(z))

However, in the case, I need to implement the columns to group by and summarize are specified as strings.

cols2group <- c("x","y")
col2summarize <- "z"

How can I get the same example as above working?


Solution

  • For this you can now use _at versions of the verbs

    df %>%  
      group_by_at(cols2group) %>% 
      summarize_at(.vars = col2summarize, .funs = min)
    

    Edit (2021-06-09):

    Please see Ronak Shah's answer, using

    mutate(across(all_of(cols2summarize), min))
    

    Now the preferred option