R

Asked • 09/11/19

Summarizing multiple columns with dplyr?

I'm struggling a bit with the dplyr-syntax. I have a data frame with different variables and one grouping variable. Now I want to calculate the mean for each column within each group, using dplyr in R. 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) ) df %>% group_by(grp) %>% summarise(mean(a))This gives me the mean for column "a" for each group indicated by "grp".My question is: is it possible to get the means for each column within each group at once? Or do I have to repeat `df %>% group_by(grp) %>% summarise(mean(a))` for each column?What I would like to have is something like df %>% group_by(grp) %>% summarise(mean(a:d)) # "mean(a:d)" does not work

Brennan H.

tutor
It's not using dplyr but perhaps a simpler way to do this is using aggregate() in the stats package. > means = aggregate(df, by=list(grp=df$grp), FUN=mean)
Report

09/12/19

2 Answers By Expert Tutors

By:

Robert S. answered • 11/22/19

Tutor
5.0 (186)

R Developer and Analyst

Ilya F. answered • 09/27/19

Tutor
New to Wyzant

PhD ecologist, data scientist, teacher

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.