Well, I know that there are already tons of related questions, but none gave an answer to my particular need.
I want to use dplyr \"summarize\" on a table with 50 co
Fortunately, there is a much simpler way available now.
With the new dplyr 1.0.0 coming out soon, you can leverage the across
function for this purpose.
All you need to type is:
iris %>%
group_by(Species) %>%
summarize(
# I want the sum over the first two columns,
across(c(1,2), sum),
# the mean over the third
across(3, mean),
# the first value for all remaining columns (after a group_by(Species))
across(-c(1:3), first)
)
Great, isn't it?
I first thought the across is not necessary as the scoped variants worked just fine, but this use case is exactly why the across
function can be very beneficial.
You can get the latest version of dplyr by devtools::install_github("tidyverse/dplyr")