【发布时间】:2018-11-14 17:01:45
【问题描述】:
我在 R 中有以下数据框:
YEAR DOY PRECTOT cumsum Lws prec0
<int> <chr> <dbl> <dbl> <chr> <chr>
1 1982 121 6.05 6.05 no no
2 1982 122 1.10 7.15 no no
3 1982 123 0.490 7.64 no no
4 1982 124 4.53 12.2 no no
5 1982 125 3.94 16.1 no no
6 1982 126 2.78 18.9 no no
7 1982 127 0.420 19.3 no no
8 1982 128 0. 19.3 no yes
9 1982 129 0.0700 19.4 no no
10 1982 130 8.94 28.3 no no
我想要另一列像 cumsum 列一样计算累积总和,然后在 PRECTOT 为 0 时重新开始计数,例如在第 8 行。基本上它应该从第 8 行重新开始累积总和,然后从那里继续累积总和,因此:
YEAR DOY PRECTOT cumsum Lws prec0
<int> <chr> <dbl> <dbl> <chr> <chr>
1 1982 121 6.05 6.05 no no
2 1982 122 1.10 7.15 no no
3 1982 123 0.490 7.64 no no
4 1982 124 4.53 12.2 no no
5 1982 125 3.94 16.1 no no
6 1982 126 2.78 18.9 no no
7 1982 127 0.420 19.3 no no
8 1982 128 0. 0 no yes
9 1982 129 0.0700 0.0700 no no
在 R 中是否有一种很好且有效的方法?谢谢。
【问题讨论】:
-
你能
dput你的数据吗?
标签: r dataframe sum restart counting