Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201894

Creating a table from summarized data using dplyr

$
0
0

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

Viewing all articles
Browse latest Browse all 201894

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>