[R] Annual cumulative sums from time series

Petr Pikal petr.pikal at precheza.cz
Thu Feb 10 11:37:17 CET 2005


Hi Georg

If you know which value belongs to which year you can use

tapply, by, aggregate family like:

years<-rep(c(2000,2001,2002,2003), each=10)
values<-(1:40)
test<-cbind(years,values)
test<-data.frame(test)
aggregate(test$values,list(y=test$years),sum)

Cheers
Petr



On 10 Feb 2005 at 11:01, Georg Hoermann wrote:

> Hello world,
> 
> I am actually transferring a course in data management for
> students in biology, geography and agriculture 
> from statistica to R - it works
> surprisingly well. If anyone is interested in my scratch/notepad
> (in German language), please see
> 
> www.hydrology.uni-kiel.de/~schorsch/statistik/statistik_datenauswertun
> g.pdf
> 
> (pages 40-52)
> 
> The dataset is:
> 
> www.hydrology.uni-kiel.de/~schorsch/statistik/erle_stat.csv
> 
> It contains a 10 year dataset. So far for introduction, now
> comes the problem:
> 
> 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. I have the
> strong hope that the solution is one of these one-liners which leave
> the students with eyes wide open in surprise and makes them true
> believers in the power of the command-line 8-).
> 
> Thanks & Greetings
> Georg
> 
> 
> -- 
> Georg Hoermann, Luebeck, Germany
> Tel. 0451/47 70 32, 0172/431 57 15, Penguin #189476
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html

Petr Pikal
petr.pikal at precheza.cz




More information about the R-help mailing list