Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass multiple column names as input to group_by in dplyr [duplicate]

Tags:

r

dplyr

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.

like image 444
nasia jaffri Avatar asked Mar 05 '17 18:03

nasia jaffri


1 Answers

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()
like image 61
Jake Kaupp Avatar answered Oct 10 '22 11:10

Jake Kaupp