[R] Using cumsum with 'group by' ?

Berend Hasselman bhh at xs4all.nl
Thu Nov 22 20:20:59 CET 2012


On 22-11-2012, at 18:08, TheRealJimShady wrote:

> Hi,
> 
> First post here. Grateful for any help you can give. I have data which looks
> like this:
> 
> id    time    x
> 1   12:01    5
> 1   12:02   14
> 1   12:03   6
> 1   12:04   3
> 2   12:01   98
> 2   12:02   23
> 2   12:03   1
> 2   12:04   4
> 3   12:01   5
> 3   12:02   65
> 3   12:03   23
> 3   12:04   23
> 
> But I want to add a column which is the cumulative sum of X, but only by id.
> I've used cumsum before, but not in this way. So the result should be
> something like:
> 
> id       time      x        cumsum
> 1      12:01       5        5
> 1      12:02      14      19 
> 1      12:03      6         25
> 1      12:04      3         28
> 2      12:01      98       98  
> 2      12:02      23       121
> 2      12:03      1         122
> 2      12:04      4         126
> 3      12:01      5          5
> 3      12:02      65       70
> 3      12:03      23       93 
> 3      12:04      23       116
> 
> Any ideas please?


Assuming your data are in a dataframe named df this should do what you want

df[,"cumsum"] <- ave(df$x,by=df$id, FUN=cumsum)


Berend




More information about the R-help mailing list