I would like to add overall summary rows while also calculating summaries by group using dplyr. I have found various questions asking how to do this, e.g. here, here, and here, but no clear solution. One possible approach is to perform count
twice and bind the rows:
mtcars %>%
count(cyl, gear) %>%
bind_rows(
count(mtcars, gear)
)
which nearly produces what I need (the left-most column has NAs rather than 'Total' or similar):
cyl gear n
<dbl> <dbl> <int>
1 4 3 1
2 4 4 8
3 4 5 2
4 6 3 2
5 6 4 4
6 6 5 1
7 8 3 12
8 8 5 2
9 NA 3 15
10 NA 4 12
11 NA 5 5
Am I missing an easier/built-in solution?
See Question&Answers more detail:os