Adding to @arkrun an answer that is not so easy to add as a comment:
Although this is a bit more complicated, this format allows for previous changes to the data frame. Useful if a longer chain of verbs exists before creating the table. (Do you want to change the names or select only certain variables)
mtcars %>% count(cyl, gear) %>% ungroup() %>% mutate(cyl=as.character(cyl)) bind_rows(group_by(.,gear) %>% summarise(n=sum(n)) %>% mutate(cyl='Total')) %>% spread(cyl) ## A tibble: 3 x 5 # gear `4` `6` `8` Total #* <dbl> <dbl> <dbl> <dbl> <dbl> #1 3 1 2 12 15 #2 4 8 4 0 12 #3 5 2 1 2 5
It can also be doubled to generate a common string for distribution.
mtcars %>% count(cyl, gear) %>% ungroup() %>% mutate(cyl=as.character(cyl), gear = as.character(gear)) %>% bind_rows(group_by(.,gear) %>% summarise(n=sum(n)) %>% mutate(cyl='Total')) %>% bind_rows(group_by(.,cyl) %>% summarise(n=sum(n)) %>% mutate(gear='Total')) %>% spread(cyl,n,fill=0)
source share