具有data.table和多个条件的累积和

时间:2014-06-03 16:28:44

标签: r data.table cumsum

我一直在与cumsum战斗,以适应多种条件。在这种情况下,我需要为yearsitecanopytreatemt_abbr执行一年中每一天的运行温度总和。以下是简化数据的示例:

  site year doy canopy power treatment_abbr airtemp_comb_nearby gdd10
  cfc 2009   1 closed     0             h2          -18.153490     0
  cfc 2009   2 closed     0             h2           18.153490     8
  cfc 2009   3 closed     0             h2           13.153490     3
  cfc 2009   1 open       0             ac           12.490        2
  cfc 2009   2 open       0             ac           16.912620     6
 hwrc 2012   1 closed     0             dc           11.146437     1
 hwrc 2012   2 closed     0             dc            2.005500     0
 hwrc 2012   3 closed     0             dc            2.5500       0
 hwrc 2012   4 closed     0             dc            22.1234      12
 hwrc 2012   5 closed     0             dc            2.005500     0

我的实际数据集非常大,所以我想使用data.table功能。在我看来,以下应该工作。它会创建新列“gddsum10”,但无法执行运行总和。知道我在这里做错了吗?

dt[order(doy), gddsum10:=cumsum(gdd10), by=c("year", "doy", "site", 
                                             "canopy", "treatment_abbr")]

我正在寻找与新列"gddsum10"

相关的内容
  site year doy canopy power treatment_abbr airtemp_comb_nearby gdd10    gddsum10
  cfc 2009   1 closed     0             h2          -18.153490     0     0
  cfc 2009   2 closed     0             h2           18.153490     8     8
  cfc 2009   3 closed     0             h2           13.153490     3     11
  cfc 2009   1 open       0             ac           12.490        2     2
  cfc 2009   2 open       0             ac           16.912620     6     8
 hwrc 2012   1 closed     0             dc           11.146437     1     1
 hwrc 2012   2 closed     0             dc            2.005500     0     1
 hwrc 2012   3 closed     0             dc            2.5500       0     1
 hwrc 2012   4 closed     0             dc            22.1234      12    13
 hwrc 2012   5 closed     0             dc            2.005500     0     13

0 个答案:

没有答案