• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Rstats 101

Learn R Programming Tips & Tricks for Statistics and Data Science

  • Home
  • About
    • Privacy Policy
  • Show Search
Hide Search

How to calculate cumulative sum in R

rstats101 · December 6, 2022 ·

In this tutorial, we will learn how to calculate cumulative sum in R. With cumsum() function in base R, we. can compute cumulative sum of a vector in R.

We will first see two examples of using cumsum() on a vector to compute cumulative sum and then we will compute cumulative sum of a column in a dataframe.

library(tidyverse)

Let us create a simple vector with sequence of numbers from 1 to 10.

1:10
##  [1]  1  2  3  4  5  6  7  8  9 10

Applying cumulative sum on the vector 1:10 using cumsum() function in R, we get

cumsum(1:10)

##  [1]  1  3  6 10 15 21 28 36 45 55

See that cumulative sum at any index is sum of all the elements including the value at the index. For example, cumulative sum at index 3 of the vector is 6, the sum of three elements 1,2, and 3.

Let us consider another example computing cumsum on a vector. And this time we use 10 random numbers generated from uniform distribution.

set.seed(1234)
runif(10)

##  [1] 0.113703411 0.622299405 0.609274733 0.623379442 0.860915384 0.640310605
##  [7] 0.009495756 0.232550506 0.666083758 0.514251141

By applying cumsum() on this random vector we get the following.

cumsum(runif(10))

##  [1] 0.6935913 1.2385661 1.5212997 2.4447332 2.7370490 3.5743447 3.8605679
##  [8] 4.1273887 4.3141115 4.5463374

We can see that when the input vector has posotive numbers cumulative sum keeps increasing.

cumulative sum of a column in a dataframe

Here we consider the example of computing cumulative sum of a column in a dataframe. For this example, we first create a dataframe with numbers from 1 to 10.

df <- tibble(x =1:10)
df
## # A tibble: 10 × 1
##        x
##    <int>
##  1     1
##  2     2
##  3     3
##  4     4
##  5     5
##  6     6
##  7     7
##  8     8
##  9     9
## 10    10

We compute cumulative sum using cumsum() function as before and use mutate() function in dplyr to add the cumulative sum as a new column to the dataframe.

df %>% 
  mutate(x_cumsum = cumsum(x))

## # A tibble: 10 × 2
##        x x_cumsum
##    <int>    <int>
##  1     1        1
##  2     2        3
##  3     3        6
##  4     4       10
##  5     5       15
##  6     6       21
##  7     7       28
##  8     8       36
##  9     9       45
## 10    10       55

We can visualise the growth cumulative sum when we sequence of numbers as in our example. Here the blue line the cumulative sum, while the dots are the sequence of numbers from 1 to 10.

cumulative sum in R
cumulative sum in R

Related

Filed Under: rstats101 Tagged With: cumulative sum in R

Primary Sidebar

Recent Posts

  • How to create a nested dataframe with lists
  • How to compute proportion with tidyverse
  • How to Compute Z-Score of Multiple Columns
  • How to drop unused level of factor variable in R
  • How to compute Z-score

Categories

%in% arrange() as.data.frame as_tibble built-in data R colSums() R cor() in R data.frame dplyr dplyr across() dplyr group_by() dplyr rename() dplyr rowwise() dplyr row_number() dplyr select() dplyr slice_max() dplyr slice_sample() drop_na R duplicated() gsub head() impute with mean values is.element() linear regression matrix() function na.omit R NAs in R near() R openxlsx pivot_longer() prod() R.version replace NA replace NAs tidyverse R Function rstats rstats101 R version scale() sessionInfo() t.test() tidyr tidyselect tidyverse write.xlsx

Copyright © 2025 · Daily Dish Pro on Genesis Framework · WordPress · Log in

Go to mobile version