[R] cum sums

David Winsemius dwinsemius at comcast.net
Mon Mar 10 13:44:12 CET 2014


Df $cumval <- ave(Df$val, Df$val, cumsum)
Df[-3]

-- 
David

> On Mar 10, 2014, at 1:57 AM, "Philip A. Viton" <viton.1 at osu.edu> wrote:
> 
> 
> Suppose I have a dataframe beginning:
> 
> id  yr   val
> a  1950   1
> b  1950   10
> a  1951   2
> 
> I'm trying to produce a table of cumulative sums
> of val, disaggregated by id and then yr, so the result
> should begin
> 
> id yr   cumval
> a  1950  1
> a  1951  3
> b  1950  10
> 
> I've been trying to do this using "aggregate"
> and passing the function "cumsum," but I can't get
> it to work. Can someone tell me how to do this?
> Thanks!
> 
> 
> ------------------------
> Philip A. Viton
> City Planning, Ohio State University
> 275 West Woodruff Avenue, Columbus OH 43210
> viton.1 at osu.edu
> 
> ______________________________________________
> 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