Skip to content
Prev 311687 / 398503 Next

Using cumsum with 'group by' ?

On Nov 23, 2012, at 12:04 , TheRealJimShady wrote:

            
Did you miss an 'e' on the first 'not'?? Otherwise, I'm confused.

Why do people always forget about ave()? I'd try

newdata <- transform(mydata, csum=ave(x, id, as.Date(date), FUN=cumsum))

You still need to sort, of course, at least by date (incl. time). Check carefully whether time zone is an issue for as.Date --- you may need the tz argument.

Also notice that I shy using an R function name as a variable name. This is mostly superstition these days, but better safe than sorry.