Calculate cumulative sum (cumsum) by group

RCumsum

R Problem Overview


With data frame:

df <- data.frame(id = rep(1:3, each = 5)
                 , hour = rep(1:5, 3)
                 , value = sample(1:15))

I want to add a cumulative sum column that matches the id:

df
   id hour value csum
1   1    1     7    7
2   1    2     9   16
3   1    3    15   31
4   1    4    11   42
5   1    5    14   56
6   2    1    10   10
7   2    2     2   12
8   2    3     5   17
9   2    4     6   23
10  2    5     4   27
11  3    1     1    1
12  3    2    13   14
13  3    3     8   22
14  3    4     3   25
15  3    5    12   37

How can I do this efficiently? Thanks!

R Solutions


Solution 1 - R

df$csum <- ave(df$value, df$id, FUN=cumsum)

ave is the "go-to" function if you want a by-group vector of equal length to an existing vector and it can be computed from those sub vectors alone. If you need by-group processing based on multiple "parallel" values, the base strategy is do.call(rbind, by(dfrm, grp, FUN)).

Solution 2 - R

To add to the alternatives, data.table's syntax is nice:

library(data.table)
DT <- data.table(df, key = "id")
DT[, csum := cumsum(value), by = key(DT)]

Or, more compactly:

library(data.table)
setDT(df)[, csum := cumsum(value), id][]

The above will:

  • Convert the data.frame to a data.table by reference
  • Calculate the cumulative sum of value grouped by id and assign it by reference
  • Print (the last [] there) the result of the entire operation

"df" will now be a data.table with a "csum" column.

Solution 3 - R

Using dplyr::

require(dplyr)
df %>% group_by(id) %>% mutate(csum = cumsum(value))

Solution 4 - R

Using library plyr.

library(plyr)
ddply(df,.(id),transform,csum=cumsum(value))

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRockView Question on Stackoverflow
Solution 1 - RIRTFMView Answer on Stackoverflow
Solution 2 - RA5C1D2H2I1M1N2O1R2T1View Answer on Stackoverflow
Solution 3 - RtjeboView Answer on Stackoverflow
Solution 4 - RDidzis ElfertsView Answer on Stackoverflow