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.