# How to find the cumulative sum for factor levels in an R data frame?

Cumulative sums are mostly used in descriptive analysis of data but sometimes we might want to calculate them in understanding the time series analysis for moving sums but it is very rare. If we have a factor column in an R data frame then it would not make sense to find the cumulative sum for all factor levels together, we must find the cumulative sums for each level. This can be easily done by using ave function.

## Example

Consider the below data frame −

Live Demo

set.seed(15)
x1<-as.factor(sample(LETTERS[1:3],20,replace=TRUE))
x2<-rpois(20,5)
df1<-data.frame(x1,x2)
df1

## Output

  x1 x2
1  A 5
2  C 6
3  B 2
4  B 3
5  A 8
6  B 5
7  A 5
8  A 3
9  C 5
10 A 2
11 C 5
12 B 6
13 A 2
14 A 5
15 A 4
16 C 6
17 B 8
18 A 5
19 B 8
20 B 3

Finding cumulative sum of x2 values for different values of x1 −

df2

## Output

  Group Response CumSum_of_GroupLevels
1  GRP2    1       1
2  GRP3    1       1
3  GRP2    8       9
4  GRP2    1       10
5  GRP2    4       14
6  GRP1    7       7
7  GRP1    8       15
8  GRP1    2       17
9  GRP1    1       18
10 GRP1    1       19
11 GRP4    3       3
12 GRP3    9       10
13 GRP4    4       7
14 GRP1    9       28
15 GRP4    5       12
16 GRP2    8       22
17 GRP2    10       32
18 GRP3    5       15
19 GRP3    8       23
20 GRP3    8       31

Updated on: 17-Oct-2020

380 Views