【问题标题】:How to apply a window function over a subset of rows inside dplyr::mutate?如何在 dplyr::mutate 内的行子集上应用窗口函数?
【发布时间】:2020-05-04 16:09:20
【问题描述】:

让我们假设mtcars 是一个有序的data.frame,我想要前几行的最大值。然后,我可以这样做:

> mtcars %>% mutate(test = cummax(wt))
    mpg cyl  disp  hp drat    wt  qsec vs am gear carb  test
1  21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4 2.620
2  21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4 2.875
3  22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1 2.875
4  21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1 3.215
5  18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2 3.440
6  18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1 3.460
7  14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4 3.570
8  24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2 3.570
9  22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2 3.570
10 19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4 3.570
11 17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4 3.570
12 16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3 4.070
13 17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3 4.070
14 15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3 4.070
15 10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4 5.250
16 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4 5.424
17 14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4 5.424
...

现在我想获得最大值,不仅仅是之前的所有行,而是之前的 wt < 4 的行,所以我会从第 12 行得到不同的结果(在下面的 test2 列中表示)对于test2,如果下一条指令有效:

> mtcars %>% mutate(test = cummax(wt), test2 = cummax(wt[wt < 4]))
    mpg cyl  disp  hp drat    wt  qsec vs am gear carb  test test2
1  21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4 2.620 2.620
2  21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4 2.875 2.875
3  22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1 2.875 2.875
4  21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1 3.215 3.215
5  18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2 3.440 3.440
6  18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1 3.460 3.460
7  14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4 3.570 3.570
8  24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2 3.570 3.570
9  22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2 3.570 3.570
10 19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4 3.570 3.570
11 17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4 3.570 3.570
12 16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3 4.070 3.570
13 17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3 4.070 3.730
14 15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3 4.070 3.780
15 10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4 5.250 3.780
16 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4 5.424 3.780
17 14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4 5.424 3.780
...

但实际上,该指令会产生错误:

> mtcars %>% mutate(test = cummax(wt[wt<4]))
Error: Column `test` must be length 32 (the number of rows) or one, not 28

让我补充一下,我想按组执行此操作,因此指令将以 mtcars %&gt;% group_by(cyl) %&gt;% ... 开头

我该怎么办?

提前谢谢你!

【问题讨论】:

    标签: r filter window-functions dplyr


    【解决方案1】:

    我会使用ifelse,并将任何违反您条件的内容替换为-Inf

    mtcars %>% 
        mutate(test = cummax(ifelse(wt < 4, wt, -Inf)))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-02
      • 2015-04-29
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 2017-11-09
      相关资源
      最近更新 更多