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.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With