How to Perform Cumulative Sum in R?

How to Perform Cumulative Sum in R?


Data created

treatment=rep(c("Cultivar A", "Cultivar B", "Cultivar C", "Cultivar D", "Cultivar E"), each=3)
rep=rep(c("I", "II", "III"), time=5)
yield=c(10, 11, 21, 13, 23, 23, 13, 13, 5, 33, 21, 13, 42, 12, 13)
nitrogen=c(500,450,482,485,770,158,565,985,458,636,965,458,565,985,458)
dataA=data.frame(treatment, rep, yield, nitrogen)

    treatment rep yield nitrogen
1  Cultivar A   I    10      500
2  Cultivar A  II    11      450
3  Cultivar A III    21      482
4  Cultivar B   I    13      485
5  Cultivar B  II    23      770
6  Cultivar B III    23      158
7  Cultivar C   I    13      565
8  Cultivar C  II    13      985
9  Cultivar C III     5      458
10 Cultivar D   I    33      636
11 Cultivar D  II    21      965
12 Cultivar D III    13      458
13 Cultivar E   I    42      565
14 Cultivar E  II    12      985
15 Cultivar E III    13      458

Cumulative Sum per group

library(dplyr)
dataB= data.frame(dataA %>%
       group_by(treatment) %>%
       mutate(yield_accu=cumsum(yield)))

dataC= data.frame(dataB %>%
       group_by(treatment) %>%
       mutate(nitrogen_accu=cumsum(nitrogen)))

dataC
    treatment rep yield nitrogen yield_accu nitrogen_accu
1  Cultivar A   I    10      500         10           500
2  Cultivar A  II    11      450         21           950
3  Cultivar A III    21      482         42          1432
4  Cultivar B   I    13      485         13           485
5  Cultivar B  II    23      770         36          1255
6  Cultivar B III    23      158         59          1413
7  Cultivar C   I    13      565         13           565
8  Cultivar C  II    13      985         26          1550
9  Cultivar C III     5      458         31          2008
10 Cultivar D   I    33      636         33           636
11 Cultivar D  II    21      965         54          1601
12 Cultivar D III    13      458         67          2059
13 Cultivar E   I    42      565         42           565
14 Cultivar E  II    12      985         54          1550
15 Cultivar E III    13      458         67          2008


Leave a Reply

If you include a website address in the comment section, I cannot see your comment as it will be automatically deleted and will not be posted. Please refrain from including website addresses.