Calculate Cumulative Sum by way of Workforce in R


You’ll be able to importance please see forms to calculate a cumulative sum by way of workforce in R:

Mode 1: Virtue Bottom R

df$cum_sum <- ave(df$values_var, df$group_var, FUN=cumsum)

Mode 2: Virtue dplyr

library(dplyr)

df %>% group_by(group_var) %>% mutate(cum_sum = cumsum(values_var))

Mode 3: Virtue information.desk

library(information.desk)

setDT(df)[, cum_sum := cumsum(values_var), group_var] 

Refer to examples display methods to importance every mode in apply with please see information body in R:

#manufacture information body
df <- information.body(bundle=rep(c('A', 'B', 'C'), every=4),
                 gross sales=c(3, 4, 4, 2, 5, 8, 9, 7, 6, 8, 3, 2))

#view information body
df

   bundle gross sales
1      A     3
2      A     4
3      A     4
4      A     2
5      B     5
6      B     8
7      B     9
8      B     7
9      C     6
10     C     8
11     C     3
12     C     2

Instance 1: Calculate Cumulative Sum by way of Workforce The usage of Bottom R

Refer to code displays methods to importance the ave() serve as from bottom R to calculate the cumulative sum of gross sales, grouped by way of bundle:

#upload column to turn cumulative gross sales by way of bundle
df$cum_sales <- ave(df$gross sales, df$bundle, FUN=cumsum)

#view up to date information body
df

   bundle gross sales cum_sales
1      A     3         3
2      A     4         7
3      A     4        11
4      A     2        13
5      B     5         5
6      B     8        13
7      B     9        22
8      B     7        29
9      C     6         6
10     C     8        14
11     C     3        17
12     C     2        19

The fresh column known as cum_sales shows the cumulative sum of gross sales, grouped by way of bundle.

Instance 2: Calculate Cumulative Sum by way of Workforce The usage of dplyr

Refer to code displays methods to importance diverse purposes from the dplyr package deal in R to calculateĀ the cumulative sum of gross sales, grouped by way of bundle:

library(dplyr)

#upload column to turn cumulative gross sales by way of bundle
df %>% group_by(bundle) %>% mutate(cum_sales = cumsum(gross sales))

#view up to date information body
df

# A tibble: 12 x 3
# Teams:   bundle [3]
   bundle gross sales cum_sales
         
 1 A         3         3
 2 A         4         7
 3 A         4        11
 4 A         2        13
 5 B         5         5
 6 B         8        13
 7 B         9        22
 8 B         7        29
 9 C         6         6
10 C         8        14
11 C         3        17
12 C         2        19

The fresh column known as cum_sales shows the cumulative sum of gross sales, grouped by way of bundle.

Instance 3: Calculate Cumulative Sum by way of Workforce The usage of information.desk

Refer to code displays methods to importance diverse purposes from the information.desk package deal in R to calculate the cumulative sum of gross sales, grouped by way of bundle:

library(information.desk)

#upload column to turn cumulative gross sales by way of bundle
setDT(df)[, cum_sales := cumsum(sales), store] 

#view up to date information body
df

    bundle gross sales cum_sales
 1:     A     3         3
 2:     A     4         7
 3:     A     4        11
 4:     A     2        13
 5:     B     5         5
 6:     B     8        13
 7:     B     9        22
 8:     B     7        29
 9:     C     6         6
10:     C     8        14
11:     C     3        17
12:     C     2        19

The fresh column known as cum_sales shows the cumulative sum of gross sales, grouped by way of bundle.

Be aware: All 3 forms put together the similar end result. On the other hand, the dplyr and knowledge.desk forms will have a tendency to be faster when operating with extraordinarily immense information frames.

Supplementary Sources

Refer to tutorials give an explanation for methods to carry out alternative ordinary calculations in R:

Calculate the Sum by way of Workforce in R
Calculate the Heartless by way of Workforce in R
Calculate Usual Divergence by way of Workforce in R

Leave a Comment