R

Cumulative sum or count in R

Here is how to calculate cumulative sum or count by using R built-in datasets.

Cumulative sum in R

Here is data from the R built-in airpassanger dataset. This data comes in time-series format and first of all, I will create a data frame.

The cumulative sum is calculated by using function cumsum.

Cummulative calculation within group in R

 

Sometimes cumulative sum is needed within the group. For example withing year, month or whatever.

You can do it by adding group_by from dplyr.

In this example, I was actually running into dplyr unused argument error, because select is also in MASS. Check out this post on how to deal with that.

Cummulative count or group index in R

 

You can do it in at least two different ways. I’m continuing the previous example.

0 comments on “Cumulative sum or count in R

Leave a Reply

Your email address will not be published. Required fields are marked *