
Summarizing multiple columns with dplyr?
2 Answers By Expert Tutors

Robert S. answered 11/22/19
R Developer and Analyst
You can use `summarise_all()` to get the means of all columns at once--you can think of it like a data-frame-friendly `sapply()`. The code block below demonstrates how to use this function in your group-by case:
```
df %>%
group_by(grp) %>%
summarise_all(mean)
```
Hope this helps!

Ilya F. answered 09/27/19
PhD ecologist, data scientist, teacher
Thanks for the reproducible example!
library(dplyr)
n = 10
df <- data.frame( a = sample(1:5, n, replace = TRUE),
b = sample(1:5, n, replace = TRUE),
c = sample(1:5, n, replace = TRUE),
d = sample(1:5, n, replace = TRUE),
grp = sample(1:3, n, replace = TRUE) )
#you can include multiple means in summarize!
df_summary <- df%>% group_by(grp) %>%
summarise(mean(a),
mean(b),
mean(c),
mean(d))
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Brennan H.
09/12/19