0

Using Tidyverse, is there a shortcut to tell summarise to apply the same function to all columns? For example, is there a way to achieve the results below without spelling out all of the column names? (In the data set I'm working with, there are a lot more columns.)

> library(tidyverse)
> mtcars %>% group_by(cyl) %>% summarise(mpg=mean(mpg), disp=mean(disp),
   hp=mean(hp), drat=mean(drat), wt=mean(wt), qsec=mean(qsec), vs=mean(vs),
  am=mean(am), gear=mean(gear), carb=mean(carb))
# A tibble: 3 x 11
    cyl   mpg  disp    hp  drat    wt  qsec    vs    am  gear  carb
  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1     4  26.7  105.  82.6  4.07  2.29  19.1 0.909 0.727  4.09  1.55
2     6  19.7  183. 122.   3.59  3.12  18.0 0.571 0.429  3.86  3.43
3     8  15.1  353. 209.   3.23  4.00  16.8 0     0.143  3.29  3.5 

If I wanted the means for the entire table, I could use sapply(mtcars, mean). But here, I want the means for each group.

pogibas
  • 27,303
  • 19
  • 84
  • 117
mhwombat
  • 8,026
  • 28
  • 53
  • 6
    Just use `summarise_all(mean)` or for selected columns `summarise_at(vars(columnnames), mean)` – akrun Aug 08 '18 at 13:03
  • 3
    `summarize_if` might also be useful if you have non numeric columns you want to ignore conveniently: `mtcars %>% group_by(cyl) %>% summarize_if(is.numeric,mean)` . Also FYI: `aggregate(. ~ cyl, mtcars, mean)` – moodymudskipper Aug 08 '18 at 13:05
  • 1
    @akrun, that's exactly what I needed. If you want to make that an answer, I'll accept it. – mhwombat Aug 08 '18 at 13:07
  • 2
    Just googled "dplyr summarise all columns" to get that duplicate. Top hit. – Axeman Aug 08 '18 at 13:14
  • @axeman It wasn't obvious to me that dplyr would provide the solution. I did try a similar query for tidyverse instead, and didn't find the answer. – mhwombat Aug 08 '18 at 14:08
  • Sorry, @mhwombat, that wasn't just directed at you. Of course, you were already using `dplyr`. It's best to use the actual package you are using when searching, not the `tidyverse` "meta-package". (See also PoGibas's edit to your tags.) For future reference! – Axeman Aug 08 '18 at 14:09

1 Answers1

2

We can use summarise_all for applying the function to all columns

library(dplyr)
mtcars %>%
     group_by(cyl) %>%
     summarise_all(mean, na.rm = TRUE)
akrun
  • 874,273
  • 37
  • 540
  • 662