gpt4 book ai didi

R:如何在不使用循环的情况下对数据框中的前一行进行计算?

转载 作者:行者123 更新时间:2023-12-04 02:48:24 24 4
gpt4 key购买 nike

如何在不使用 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 中也执行上述操作会很好。

最佳答案

为此你需要使用 cumsum:

mydata$cumHDD<-with(mydata,cumsum(HeatingDegreeDay))

假设 mydata 是你的数据。

使用 ddply 的 mtcars 数据示例

library(plyr)
mydata<-mtcars
k<-ddply(mydata,.(cyl),summarize,cummpg=cumsum(mpg))

head(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

关于R:如何在不使用循环的情况下对数据框中的前一行进行计算?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18341787/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com