[R] aggregate with cumsum

Greg Snow Greg.Snow at imail.org
Tue Oct 12 20:01:57 CEST 2010


Try ave instead of aggregate.  If that does not do it, then look at the plyr package, probably the ddply function in that package.

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Bond, Stephen
> Sent: Tuesday, October 12, 2010 11:40 AM
> To: r-help at r-project.org
> Subject: [R] aggregate with cumsum
> 
> Hello everybody,
> 
> Data is
> myd <-
> data.frame(id1=rep(c("a","b","c"),each=3),id2=rep(1:3,3),val=rnorm(9))
> 
> I want to get a cumulative sum over each of id1. trying aggregate does
> not work
> 
> myd$pcum <- aggregate(myd[,c("val")],list(orig=myd$id1),cumsum)
> 
> Please suggest a solution. In real the dataframe is huge so looping
> with for and subsetting is not a great idea (still doable, though).
> Thank you
> 
> 
> Stephen B
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list