【发布时间】:2018-07-06 18:41:23
【问题描述】:
我的excel数据集如下:
Weight Quantity Price
72 5 460
73 8 720
75 20 830
95 2 490
91 15 680
82 14 340
88 30 250
89 6 770
78 27 820
98 24 940
99 29 825
我想获得一个权重与数量数据透视表,其中每个类别的价格总和如下:
0-10 10-20 20-30
70-80 1180 830 820
80-90 770 340 250
90-100 490 680 1765
我使用dplyr 包为各个类别创建了两个表来获取平均值和计数,如下所示:
table1 <- group_by(dataset, Weight = cut(Weight, breaks = c(70,80,90,100))
result1 <- summarise(table1, Count = n(), Avg_Price = mean(Price, na.rm = T))
table2 <- group_by(dataset, Quantity = cut(Quantity, breaks = c(0,10,20,30))
result2 <- summarise(table2, Count = n(), Avg_Price = mean(Price, na.rm = T))
现在,我如何使用 table1 和 table2 来创建一个交叉表?
【问题讨论】:
标签: r pivot-table