Calculate cumsum by Groups of Data Frame in R (Example Code)
This article explains how to compute the cumsum by groups in a data frame in the R programming language.
Construction of Example Data
data(iris) # Load example data iris_sub <- iris[c(1, 2, 51, 52, 101, 102), ] iris_sub # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 1 5.1 3.5 1.4 0.2 setosa # 2 4.9 3.0 1.4 0.2 setosa # 51 7.0 3.2 4.7 1.4 versicolor # 52 6.4 3.2 4.5 1.5 versicolor # 101 6.3 3.3 6.0 2.5 virginica # 102 5.8 2.7 5.1 1.9 virginica |
data(iris) # Load example data iris_sub <- iris[c(1, 2, 51, 52, 101, 102), ] iris_sub # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 1 5.1 3.5 1.4 0.2 setosa # 2 4.9 3.0 1.4 0.2 setosa # 51 7.0 3.2 4.7 1.4 versicolor # 52 6.4 3.2 4.5 1.5 versicolor # 101 6.3 3.3 6.0 2.5 virginica # 102 5.8 2.7 5.1 1.9 virginica
Example: Calculate Cumulative Sum by Group Using group_by & mutate Functions of dplyr Package
install.packages("dplyr") # Install & load dplyr library("dplyr") |
install.packages("dplyr") # Install & load dplyr library("dplyr")
iris_cumsum <- iris_sub %>% # Create cumsum column group_by(Species) %>% dplyr::mutate(SL_cs = cumsum(Sepal.Length)) head(iris_cumsum) # # A tibble: 6 × 6 # # Groups: Species [3] # Sepal.Length Sepal.Width Petal.Length Petal.Width Species SL_cs # <dbl> <dbl> <dbl> <dbl> <fct> <dbl> # 1 5.1 3.5 1.4 0.2 setosa 5.1 # 2 4.9 3 1.4 0.2 setosa 10 # 3 7 3.2 4.7 1.4 versicolor 7 # 4 6.4 3.2 4.5 1.5 versicolor 13.4 # 5 6.3 3.3 6 2.5 virginica 6.3 # 6 5.8 2.7 5.1 1.9 virginica 12.1 |
iris_cumsum <- iris_sub %>% # Create cumsum column group_by(Species) %>% dplyr::mutate(SL_cs = cumsum(Sepal.Length)) head(iris_cumsum) # # A tibble: 6 × 6 # # Groups: Species [3] # Sepal.Length Sepal.Width Petal.Length Petal.Width Species SL_cs # <dbl> <dbl> <dbl> <dbl> <fct> <dbl> # 1 5.1 3.5 1.4 0.2 setosa 5.1 # 2 4.9 3 1.4 0.2 setosa 10 # 3 7 3.2 4.7 1.4 versicolor 7 # 4 6.4 3.2 4.5 1.5 versicolor 13.4 # 5 6.3 3.3 6 2.5 virginica 6.3 # 6 5.8 2.7 5.1 1.9 virginica 12.1