如何在不使用For循环语句的情况下计算累积加热度天数?我是R的新手,我不太清楚如何完成这件事。
我想这样做:
for(i in 1:nrow(df)) {
df$cumHDD <- df$HeatingDegreeDay[i] + df$cumHDD[i-1]
i <- i+1
}
以下是我的数据。
SiteID SubstrateID WinterID Date HeatingDegreeDay cumHDD
1 NW_SB B 2002_2003 2002-11-01 0.0000000 0
2 NW_SB B 2002_2003 2002-11-02 0.0000000 0
3 NW_SB B 2002_2003 2002-11-03 0.0000000 0
4 NW_SB B 2002_2003 2002-11-04 0.0000000 0
5 NW_SB B 2002_2003 2002-11-05 0.0000000 0
6 NW_SB B 2002_2003 2002-11-06 0.0000000 0
7 NW_SB B 2002_2003 2002-11-07 0.0000000 0
8 NW_SB B 2002_2003 2002-11-08 0.0000000 0
9 NW_SB B 2002_2003 2002-11-09 0.0000000 0
10 NW_SB B 2002_2003 2002-11-10 0.0000000 0
11 NW_SB B 2002_2003 2002-11-11 0.0000000 0
12 NW_SB B 2002_2003 2002-11-12 0.0000000 0
13 NW_SB B 2002_2003 2002-11-13 0.0000000 0
14 NW_SB B 2002_2003 2002-11-14 0.0000000 0
15 NW_SB B 2002_2003 2002-11-15 0.0000000 0
16 NW_SB B 2002_2003 2002-11-16 0.0000000 0
17 NW_SB B 2002_2003 2002-11-17 0.0000000 0
18 NW_SB B 2002_2003 2002-11-18 0.0000000 0
19 NW_SB B 2002_2003 2002-11-19 0.3724242 0
20 NW_SB B 2002_2003 2002-11-20 0.0000000 0
21 NW_SB B 2002_2003 2002-11-21 0.0000000 0
22 NW_SB B 2002_2003 2002-11-22 0.0000000 0
23 NW_SB B 2002_2003 2002-11-23 0.2773077 0
24 NW_SB B 2002_2003 2002-11-24 0.2679310 0
25 NW_SB B 2002_2003 2002-11-25 0.0000000 0
因此,对于我上面的数据,当它正确完成时,它应该如下所示:
SiteID SubstrateID WinterID Date HeatingDegreeDay cumHDD
1 NW_SB B 2002_2003 2002-11-01 0.0000000 0
2 NW_SB B 2002_2003 2002-11-02 0.0000000 0
3 NW_SB B 2002_2003 2002-11-03 0.0000000 0
4 NW_SB B 2002_2003 2002-11-04 0.0000000 0
5 NW_SB B 2002_2003 2002-11-05 0.0000000 0
6 NW_SB B 2002_2003 2002-11-06 0.0000000 0
7 NW_SB B 2002_2003 2002-11-07 0.0000000 0
8 NW_SB B 2002_2003 2002-11-08 0.0000000 0
9 NW_SB B 2002_2003 2002-11-09 0.0000000 0
10 NW_SB B 2002_2003 2002-11-10 0.0000000 0
11 NW_SB B 2002_2003 2002-11-11 0.0000000 0
12 NW_SB B 2002_2003 2002-11-12 0.0000000 0
13 NW_SB B 2002_2003 2002-11-13 0.0000000 0
14 NW_SB B 2002_2003 2002-11-14 0.0000000 0
15 NW_SB B 2002_2003 2002-11-15 0.0000000 0
16 NW_SB B 2002_2003 2002-11-16 0.0000000 0
17 NW_SB B 2002_2003 2002-11-17 0.0000000 0
18 NW_SB B 2002_2003 2002-11-18 0.0000000 0
19 NW_SB B 2002_2003 2002-11-19 0.3724242 0.3724242
20 NW_SB B 2002_2003 2002-11-20 0.0000000 0.3724242
21 NW_SB B 2002_2003 2002-11-21 0.0000000 0.3724242
22 NW_SB B 2002_2003 2002-11-22 0.0000000 0.3724242
23 NW_SB B 2002_2003 2002-11-23 0.2773077 0.6497319
24 NW_SB B 2002_2003 2002-11-24 0.2679310 0.9176629
25 NW_SB B 2002_2003 2002-11-25 0.0000000 0.9176629
我可以在ddply
声明中执行此操作吗?我还有其他一些功能要做,并且认为在ddply
内完成上述操作会很好。
答案 0 :(得分:4)
您需要使用cumsum
:
mydata$cumHDD<-with(mydata,cumsum(HeatingDegreeDay))
假设mydata是你的数据。
使用ddply
library(plyr)
mydata<-mtcars
k<-ddply(mydata,.(cyl),summarize,cummpg=cumsum(mpg))
头(K,10)
cyl cummpg
1 4 22.8
2 4 47.2
3 4 70.0
4 4 102.4
5 4 132.8
6 4 166.7
7 4 188.2
8 4 215.5
9 4 241.5
10 4 271.9