[R] Annual cumulative sums from time series

Peter Dalgaard p.dalgaard at biostat.ku.dk
Thu Feb 10 12:09:58 CET 2005


"Dimitris Rizopoulos" <dimitris.rizopoulos at med.kuleuven.ac.be> writes:

> maybe something like this:
> 
> dat <- data.frame(years=rep(1994:2004, each=10), x=rnorm(110),

(I thought they abolished the 10 month year after the failure of the
French revolution. :-) )

> y=rnorm(110))
> lapply(split(dat[,-1], dat$years), cumsum)

or to get a matching data frame:

 yr <- dat$years
 cum <- dat[-1]
 split(cum,yr) <- lapply(split(cum,yr),cumsum)

(or do.call("cbind",  lapply(split(dat[,-1], dat$years), cumsum)), if
you can rely on data being in group order.)


-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list