I am trying to use group_by and summarise with several columns, which will be interactive in a shiny app. So, if I specify the column names inside group_by function it works, but if I create a vector for column names, so it does not work anymore. Here follows an example with Iris dataset:
# Working
iris %>% group_by(Sepal.Length, Species) %>%
summarise(
`Sum.Sepal.Width` = sum(Sepal.Width)
)
# Not working
columns <- c("Sepal.Length", "Species")
iris %>% group_by(columns) %>%
summarise(
`Sum.Sepal.Width` = sum(Sepal.Width)
)
Thanks. Wlademir.
As mentioned in this question, it should work by using group_by_at()
:
columns <- c("Sepal.Length", "Species")
iris %>% group_by_at(columns) %>%
summarise(
"Sum.Sepal.Width" = sum(Sepal.Width)
)
Error in grouped_df_impl(data, unname(vars), drop) :
columns <- c("Sepal.Length", "Species")
iris %>% group_by(.dots = columns) %>%
summarise(
`Sum.Sepal.Width` = sum(Sepal.Width)
)
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