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.