6

I am new to R and dplyr package. I am trying to pass a variable to dplyr group_by, which we can vary/change. for instance when working with flights dataset, I can get counts of rows by any column (or multiple columns) using the code below:

library(nycflights13)
flights %>% group_by(origin) %>% tally()
flights %>% group_by(carrier) %>% tally()
flights %>% group_by(origin,carrier) %>% tally()

but if I want to pass the name of the columns used, to group_by as a variable, then it does not work when using multiple column names.

group="carrier"
flights %>% group_by_(group) %>% tally()

group="origin"
flights %>% group_by_(group) %>% tally()

group=c("origin","carrier") #This does not work
flights %>% group_by_(group) %>% tally()

I will appreciate any help. Thanks.

JelenaČuklina
  • 3,574
  • 2
  • 22
  • 35
nasia jaffri
  • 803
  • 2
  • 12
  • 21

1 Answers1

18

You've almost got it, you just need to use the .dots argument to pass in your grouping variables.

group <- c("origin","carrier") 

flights %>% 
  group_by_(.dots = group) %>% 
  tally()
Jake Kaupp
  • 7,892
  • 2
  • 26
  • 36
  • Great. Thanks a lot. Can you please help me understand what does .dots argument do. – nasia jaffri Mar 05 '17 at 19:25
  • You use the `.dots` argument for programming with `dplyr`, it enables you to pass the what you want to do, into the `dplyr` SE functions programatically. You should read the dplyr vignette on NSE for more information. – Jake Kaupp Mar 05 '17 at 19:40
  • Thanks for your help! – nasia jaffri Mar 06 '17 at 01:32
  • 2
    Update: The `.dots` argument of `group_by()` is deprecated as of dplyr 1.0.0. Instead you can use the big bank operator `!!!` as in `group_by(!!!groups)`. – ESELIA Nov 24 '21 at 15:11
  • You might need 'syms' if using the triple bang. https://stackoverflow.com/questions/61180201/triple-exclamation-marks-on-r – william3031 Aug 21 '23 at 00:13