Home Summarizing multiple columns with dplyr?
Reply: 0

Summarizing multiple columns with dplyr?

user1587 Published in July 18, 2018, 10:21 pm

This question already has an answer here:

  • Aggregate / summarize multiple variables per group (e.g. sum, mean) 4 answers
  • Can dplyr summarise over several variables without listing each one? [duplicate] 2 answers

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
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.414798 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO