[R] Annual cumulative sums from time series
Brahm, David
David.Brahm at geodecapital.com
Thu Feb 10 17:20:19 CET 2005
Georg Hoermann [mailto:georg.hoermann at gmx.de] wrote:
> www.hydrology.uni-kiel.de/~schorsch/statistik/erle_stat.csv ...
contains a 10 year dataset.
> We often need cumulative *annual* sums (sunshine, precipitation), i.e.
the sum must reset to 0
> at the beginning of the year. I know of cumsum(), but I do not now how
to split the dataset
> automagically into annual pieces so I can cumsum() every year
separately.
Several replies have been given using tapply/aggregate/split; here is a
more primitive approach.
I do the cumsum once for the series, and call it "x". Then I subtract
from x the value of x on 12/31
of the prior year:
R> y <- read.csv("erle_stat.csv", as.is=TRUE)
R> year <- substring(y$DATUM, 7)
R> x <- cumsum(y$Peff)
R> y$cumPeff <- x - c(0,x)[match(year, year)]
-- David Brahm (brahm at alum.mit.edu)
More information about the R-help
mailing list