Calculate Cumulative Sum of a Numeric Object in R Programming – cumsum() Function

cumsum() function in R Language is used to calculate the cumulative sum of the vector passed as argument.

Syntax: cumsum(x)

Parameters:
x: Numeric Object

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to illustrate 
# the use of cumsum() Function
  
# Calling cumsum() Function
cumsum(1:4)
cumsum(-1:-6)

chevron_right


Output:

[1]  1  3  6 10
[1]  -1  -3  -6 -10 -15 -21

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to illustrate 
# the use of cumsum() Function
  
# Creating Vectors
x1 <- c(2, 4, 5, 7)
x2 <- c(2.4, 5.6, 3.4)
  
# Calling cumsum() Function
cumsum(x1)
cumsum(x2)

chevron_right


Output:

[1]  2  6 11 18
[1]  2.4  8.0 11.4
My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.