变量值的累积和

时间:2014-02-03 13:31:31

标签: r sum

我有以下数据,我希望'SubstResp''Soil'累积'Added'的值。最后,我想获得新变量'SubstRespAcc'

输入数据

Stock   Soil    Added   Acctime SubstResp
A   Clayey  70.2    1   0.21300216
A   Sandy   70.2    1   4.93108145
A   Clayey  701.8   1   7.21646586
A   Sandy   701.8   1   0.63856242
A   Clayey  7017.8  1   4.78288286
A   Sandy   7017.8  1   2.67393943
B   Clayey  70.2    8   1.32209361
B   Sandy   70.2    8   4.48684094
B   Clayey  701.8   8   70.97613913
B   Sandy   701.8   8   1.68302792
B   Clayey  7017.8  8   8.8713402
B   Sandy   7017.8  8   89.58014609
C   Clayey  70.2    23  0.09295716
C   Sandy   70.2    23  3.04399183
C   Clayey  701.8   23  50.01275949
C   Sandy   701.8   23  0.71204003
C   Clayey  7017.8  23  3.23769206
C   Sandy   7017.8  23  38.18923005

期望的输出

Stock   Soil    Added   Acctime SubstResp   SubstRespAcc
A   Clayey  70.2    1   0.21300216  0.21300216
A   Sandy   70.2    1   4.93108145  4.93108145
A   Clayey  701.8   1   7.21646586  7.21646586
A   Sandy   701.8   1   0.63856242  0.63856242
A   Clayey  7017.8  1   4.78288286  4.78288286
A   Sandy   7017.8  1   2.67393943  2.67393943
B   Clayey  70.2    8   1.32209361  1.53509577
B   Sandy   70.2    8   4.48684094  9.41792239
B   Clayey  701.8   8   70.97613913 78.19260499
B   Sandy   701.8   8   1.68302792  2.32159034
B   Clayey  7017.8  8   8.8713402   13.65422306
B   Sandy   7017.8  8   89.58014609 92.25408552
C   Clayey  70.2    23  0.09295716  1.62805293
C   Sandy   70.2    23  3.04399183  12.46191422
C   Clayey  701.8   23  50.01275949 128.2053645
C   Sandy   701.8   23  0.71204003  3.03363037
C   Clayey  7017.8  23  3.23769206  16.89191512
C   Sandy   7017.8  23  38.18923005 130.4433156

非常感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

您可以使用ddply包中的plyr功能:

library(plyr)
ddply(dat, .(Soil, Added), mutate, SubstRespAcc = cumsum(SubstResp))

您还可以使用data.table

library(data.table)
datDT <- as.data.table(dat)
datDT[ , SubstRespAcc := cumsum(SubstResp), by = c("Soil", "Added")]

如果您想要基础R中的解决方案,可以使用ave

transform(dat, SubstRespAcc = ave(SubstResp, Soil, Added, FUN = cumsum))

答案 1 :(得分:0)

zz <- "Stock   Soil    Added   Acctime SubstResp
A   Clayey  70.2    1   0.21300216
A   Sandy   70.2    1   4.93108145
A   Clayey  701.8   1   7.21646586
A   Sandy   701.8   1   0.63856242
A   Clayey  7017.8  1   4.78288286
A   Sandy   7017.8  1   2.67393943
B   Clayey  70.2    8   1.32209361
B   Sandy   70.2    8   4.48684094
B   Clayey  701.8   8   70.97613913
B   Sandy   701.8   8   1.68302792
B   Clayey  7017.8  8   8.8713402
B   Sandy   7017.8  8   89.58014609
C   Clayey  70.2    23  0.09295716
C   Sandy   70.2    23  3.04399183
C   Clayey  701.8   23  50.01275949
C   Sandy   701.8   23  0.71204003
C   Clayey  7017.8  23  3.23769206
C   Sandy   7017.8  23  38.18923005"
Data <- read.table(text=zz, header = TRUE)  
library(reshape)
mydata <- aggregate(Data$SubstResp, list(Data$Soil,Data$Added), cumsum)
melt(mydata, id=(c("Group.1", "Group.2")))
   Group.1 Group.2 variable       value
1   Clayey    70.2        x   0.2130022
2    Sandy    70.2        x   4.9310814
3   Clayey   701.8        x   7.2164659
4    Sandy   701.8        x   0.6385624
5   Clayey  7017.8        x   4.7828829
6    Sandy  7017.8        x   2.6739394
7   Clayey    70.2        x   1.5350958
8    Sandy    70.2        x   9.4179224
9   Clayey   701.8        x  78.1926050
10   Sandy   701.8        x   2.3215903
11  Clayey  7017.8        x  13.6542231
12   Sandy  7017.8        x  92.2540855
13  Clayey    70.2        x   1.6280529
14   Sandy    70.2        x  12.4619142
15  Clayey   701.8        x 128.2053645
16   Sandy   701.8        x   3.0336304
17  Clayey  7017.8        x  16.8919151
18   Sandy  7017.8        x 130.4433156