4

我在 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 中是否有一种很好且有效的方法?谢谢你。

4

2 回答 2

4

“满足条件时重新启动”部分使用以下命令完成group_by(cumsum(<condition>))

library(dplyr)

dat %>% 
  group_by(grp = cumsum(PRECTOT == 0)) %>% 
  mutate(cumsum = cumsum(PRECTOT))

# # A tibble: 10 x 7
# # Groups:   grp [2]
#     YEAR DOY   PRECTOT cumsum Lws   prec0   grp
#    <int> <chr>   <dbl>  <dbl> <chr> <chr> <int>
#  1  1982 121      6.05   6.05 no    no        0
#  2  1982 122      1.1    7.15 no    no        0
#  3  1982 123      0.49   7.64 no    no        0
#  4  1982 124      4.53  12.2  no    no        0
#  5  1982 125      3.94  16.1  no    no        0
#  6  1982 126      2.78  18.9  no    no        0
#  7  1982 127      0.42  19.3  no    no        0
#  8  1982 128      0      0    no    yes       1
#  9  1982 129      0.07   0.07 no    no        1
# 10  1982 130      8.94   9.01 no    no        1

数据:

dat <- readr::read_table2(
"YEAR DOY   PRECTOT cumsum Lws   prec0
1982 121    6.05     6.05 no    no
1982 122    1.10     7.15 no    no
1982 123    0.490    7.64 no    no
1982 124    4.53    12.2  no    no
1982 125    3.94    16.1  no    no
1982 126    2.78    18.9  no    no
1982 127    0.420   19.3  no    no
1982 128    0.      19.3  no    yes
1982 129    0.0700  19.4  no    no
1982 130    8.94    28.3  no    no
", col_types = "icddcc")
于 2018-06-05T09:12:50.433 回答
2

这是一种在满足条件时使用 data.table 重新开始累积和的方法:

dat <- read.table(header = TRUE, text = "YEAR DOY   PRECTOT cumsum Lws   prec0
1982 121    6.05     6.05 no    no
1982 122    1.10     7.15 no    no
1982 123    0.490    7.64 no    no
1982 124    4.53    12.2  no    no
1982 125    3.94    16.1  no    no
1982 126    2.78    18.9  no    no
1982 127    0.420   19.3  no    no
1982 128    0.      19.3  no    yes
1982 129    0.0700  19.4  no    no
1982 130    8.94    28.3  no    no")

library(data.table)
dat <- data.table(dat)
dat[, NEWCOL:=cumsum(PRECTOT), by=cumsum(PRECTOT==0)]

使用 data.table group by ( ) 重新开始累积总和by=cumsum(<condition>)

于 2018-06-05T10:49:07.927 回答