【问题标题】:R - How to use cumulative sum by year and restart cumulative sum when condition is metR - 如何按年使用累积总和并在满足条件时重新启动累积总和
【发布时间】: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 中是否有一种很好且有效的方法?谢谢。

【问题讨论】:

标签: r dataframe sum restart counting


【解决方案1】:

“满足条件时重新启动”部分使用group_by(cumsum(&lt;condition&gt;))

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")

【讨论】:

    【解决方案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(&lt;condition&gt;)) 重新开始累积总和。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-15
      • 1970-01-01
      • 1970-01-01
      • 2018-03-02
      • 2021-06-18
      相关资源
      最近更新 更多