I have a dataframe where the column size can be grouped. When the dataframe is arranged by size, I would like to show the totals of each column for each group as a row in the df.
library(dplyr)
dat <- data.frame(size = c("S", "XS", "L", "M", "L", "L", "XS"),
category = c("shirt", "shirt", "shirt", "shirt", "pants", "hat", "pants"),
store1 = c(22, 3, 52, 10, 5, 37, 21),
store2 = c(43, 13, 2, 24, 6, 12, 40))
dat %>%
arrange(size)
size category store1 store2
1 L shirt 52 2
2 L pants 5 6
3 L hat 37 12
4 M shirt 10 24
5 S shirt 22 43
6 XS shirt 3 13
7 XS pants 21 40
I would like to get something like this
size category store1 store2
1 L shirt 52 2
2 L pants 5 6
3 L hat 37 12
4 Total L 94 20
5 M shirt 10 24
6 Total M 10 24
7 S shirt 22 43
8 Total S 22 43
9 XS shirt 3 13
10 XS pants 21 40
11 Total XS 24 53
Any suggestions are appreciated!