I want to create a summary table from summarized data using dplyr.
library(dplyr)
mtcars %>% group_by(cyl, gear) %>% summarise(avg_wt = mean(wt))
Here's the output:
# A tibble: 8 x 3
# Groups: cyl [3]
cyl gear avg_wt
<dbl> <dbl> <dbl>
1 4 3 2.46
2 4 4 2.38
3 4 5 1.83
4 6 3 3.34
5 6 4 3.09
6 6 5 2.77
7 8 3 4.10
8 8 5 3.37
How can I generate this output?
columns are cyl and rows are gear:
4 6 8
3 2.46 3.34 4.10
4 2.38 3.09 NA
5 1.83 2.77 3.37