Calculate cumulative sum (cumsum) by group

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)).


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.


Using dplyr::

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

Using library plyr.

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