# How to find the cumulative sum for each row in an R data frame?

R ProgrammingServer Side ProgrammingProgramming

To find the cumulative sum for each row in an R data frame, we would need to read the data frame as a data.table object and then Reduce function will be used with accumulate argument.

For Example, if we have a data frame called df and we want to find the cumulative sum for each row in df then we can use the below mentioned command −

setDT(df)[,names(df):=Reduce("+",df,accumulate=TRUE)]

## Example 1

Following snippet creates a sample data frame −

x1<-sample(1:50,20)
x2<-sample(1:50,20)
x3<-sample(1:50,20)
df1<-data.frame(x1,x2,x3)
df1

The following dataframe is created

  x1  x2 x3
1 6  19 3
2 28 46 5
3 17 3  16
4 16 44 25
5 50 26 14
6 40 16 11
7 21 8  48
8 19 9  27
9 37 43 1
10 34 33 15
11 4  17 50
12 48 25 44
13 31 18 12
14 7  13 9
15 12 6  8
16 22 2  4
17 23 23 41
18 11 37 19
19 49 7  13
20 3  4  49

To load data.table package and find the cumulative for each row in df1 on the above created data frame, add the following code to the above snippet −

x1<-sample(1:50,20)
x2<-sample(1:50,20)
x3<-sample(1:50,20)
df1<-data.frame(x1,x2,x3)
library(data.table)
setDT(df1)[,names(df1):=Reduce("+",df1,accumulate=TRUE)]
df1

## Output

If you execute all the above given snippets as a single program, it generates the following Output −

   x1  x2 x3
1: 6  25 28
2: 28 74 79
3: 17 20 36
4: 16 60 85
5: 50 76 90
6: 40 56 67
7: 21 29 77
8: 19 28 55
9: 37 80 81
10: 34 67 82
11: 4  21 71
12: 48 73 117
13: 31 49 61
14: 7  20 29
15: 12 18 26
16: 22 24 28
17: 23 46 87
18: 11 48 67
19: 49 56 69
20: 3  7  56

## Example 2

Following snippet creates a sample data frame −

y1<-round(rnorm(20),0)
y2<-round(rnorm(20),0)
y3<-round(rnorm(20),0)
df2<-data.frame(y1,y2,y3)
df2

The following dataframe is created

  y1  y2 y3
1  0  1  1
2 -1 -1  1
3  1 -1 -1
4  0  0  0
5 -1  0  0
6  0 -2  0
7 -1  0  0
8  1  0 -1
9  0  0  2
10  1  0  0
11 -1  0 -1
12 -1  0 -1
13  1  0  1
14  0 -2  0
15 -1  0  0
16 -1  1 -1
17 -1 -1 -1
18  0  0 -1
19  0 -1  1
20  0 -1 -1

To find the cumulative for each row in df2 on the above created data frame, add the following code to the above snippet −

y1<-round(rnorm(20),0)
y2<-round(rnorm(20),0)
y3<-round(rnorm(20),0)
df2<-data.frame(y1,y2,y3)
setDT(df2)[,names(df2):=Reduce("+",df2,accumulate=TRUE)]
df2

## Output

If you execute all the above given snippets as a single program, it generates the following Output −

    y1 y2 y3
1:  0  1  2
2: -1 -2 -1
3:  1  0 -1
4:  0  0  0
5: -1 -1 -1
6:  0 -2 -2
7: -1 -1 -1
8:  1  1  0
9:  0  0  2
10:  1  1  1
11: -1 -1 -2
12: -1 -1 -2
13:  1  1  2
14:  0 -2 -2
15: -1 -1 -1
16: -1  0 -1
17: -1 -2 -3
18:  0  0 -1
19:  0 -1  0
20:  0 -1 -2
Published on 09-Nov-2021 07:20:02