如何“按桶”计算合计值?

时间:2018-10-01 00:32:38

标签: r aggregate

我想通过“存储” R中的按值来计算数据集的合计值。我不确定这叫什么,但是请允许我使用example来举例说明{ {1}}数据集。如果我运行

mtcars

我得到:

aggregate(mtcars$mpg, by = list(disp = mtcars$disp), median)

这是排量的汽车的mpg。但是,每一个位移都是结果。如何运行此功能,以便将位移分组到几个桶中,例如每50个或20个桶?类似于以下内容:

    disp    x
1   71.1 33.9
2   75.7 30.4
3   78.7 32.4
4   79.0 27.3
5   95.1 30.4

2 个答案:

答案 0 :(得分:2)

mtcars %>%
  group_by(disp = cut(disp, breaks = 20*0:100)) %>%
  summarize(x = median(mpg))

# A tibble: 15 x 2
   disp          x
   <fct>     <dbl>
 1 (60,80]    31.4
 2 (80,100]   30.4
 3 (100,120]  22.8
 4 (120,140]  21.5
 5 (140,160]  21  
 6 (160,180]  18.5
 7 (220,240]  18.1
 8 (240,260]  21.4
 9 (260,280]  16.4
10 (300,320]  15.2
11 (340,360]  15.0
12 (380,400]  19.2
13 (420,440]  14.7
14 (440,460]  10.4
15 (460,480]  10.4

答案 1 :(得分:0)

出于完整性考虑,采用data.table方法:

require(data.table)
setDT(mtcars)
mtcars[, median(mpg), by = .(dispRanges = cut(disp, 5))]