How to create a column in an R data frame with cumulative sum?

R ProgrammingServer Side ProgrammingProgramming

The cumulative sum is used to determine the total sum of a variable or group and helps us to understand the changes in the values of that variable or group over time. While creating the cumulative, we must be sure that the total sum and the cumulative sum of the last value (depending on the direction of sum) are same. We can use mutate function of dplyr package to find the cumulative and create a column for it.

Example

Consider the below data frame −

x1 <-1:20
grp <-rep(LETTERS[1:4],each=5)
df <-data.frame(x1,grp)
df

Output

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

Creating a column cumusum with cumulative sum of values in x1 per grp −

Example

df%>%group_by(grp)%>%mutate(cumusum=cumsum(x1))
# A tibble: 20 x 3
# Groups: grp [4]
x1 grp cumusum

Output

<int> <fct> <int>
1 1 A 1
2 2 A 3
3 3 A 6
4 4 A 10
5 5 A 15
6 6 B 6
7 7 B 13
8 8 B 21
9 9 B 30
10 10 B 40
11 11 C 11
12 12 C 23
13 13 C 36
14 14 C 50
15 15 C 65
16 16 D 16
17 17 D 33
18 18 D 51
19 19 D 70
20 20 D 90

Lets’ have a look at another example −

Example

set.seed(1)
Class <-rep(c("C1","C2","C3","C4","C5"),each=4)
Frequency <-sample(1:100,20)
data <-data.frame(Class,Frequency)
data

Output

Class Frequency
1 C1 68
2 C1 39
3 C1 1
4 C1 34
5 C2 87
6 C2 43
7 C2 14
8 C2 82
9 C3 59
10 C3 51
11 C3 85
12 C3 21
13 C4 54
14 C4 74
15 C4 7
16 C4 73
17 C5 79
18 C5 37
19 C5 83
20 C5 97

Creating a column cumusum with cumulative sum of values in Frequency per Class −

Example

data%>%group_by(Class)%>%mutate(cumusum=cumsum(Frequency))
# A tibble: 20 x 3
# Groups: Class [5]
Class Frequency cumusum

Output

<fct> <int> <int>
1 C1 68 68
2 C1 39 107
3 C1 1 108
4 C1 34 142
5 C2 87 87
6 C2 43 130
7 C2 14 144
8 C2 82 226
9 C3 59 59
10 C3 51 110
11 C3 85 195
12 C3 21 216
13 C4 54 54
14 C4 74 128
15 C4 7 135
16 C4 73 208
17 C5 79 79
18 C5 37 116
19 C5 83 199
20 C5 97 296
raja
Published on 24-Aug-2020 13:08:40
Advertisements