[R] Annual cumulative sums from time series - SOLVED + questions

Georg Hoermann georg.hoermann at gmx.de
Thu Feb 10 21:26:57 CET 2005


Roger Bivand wrote:
> On Thu, 10 Feb 2005, Georg Hoermann wrote:
> 
Hello world,

based on the code of Roger I have now two solutions:

the first one (one line for the whole dataset)
--------- cut here ------

erle <- 
read.csv(url("http://www.hydrology.uni-kiel.de/~schorsch/statistik/erle_stat.csv"))
jahre <- factor(substring(erle$DATUM, 7))
tpks1 <- unlist(tapply(erle$Sonnen, jahre, cumsum))
plot(tpks1, type="l")

--------- cut here -------

The second one plots one line for each year:

---- start - cut here ----
# read data in from Internet
erle <- 
read.csv(url("http://www.hydrology.uni-kiel.de/~schorsch/statistik/erle_stat.csv"))
# extract Year as a factor from variable DATUM
jahre <- factor(substring(erle$DATUM, 7))
try1 <- tapply(erle$Sonnen, jahre, cumsum)
# create colors for every year from rainbow color scheme
cols <- rainbow(length(try1))
plot(x=c(1,366), y=c(0,1200), type="n", xlab="Days", ylab="Cumulative 
sunshine (h)")
# draw the lines, one line for each year
for (i in 1:length(try1)) lines(1:length(try1[[i]]), try1[[i]], col=cols[i])

# ...and the legend
legend(c(0,100), c(400,1100), names(try1), col=cols, lwd=1, bty="n")

---- end cut here -----

For the second example, a mean sum for all years would also be a good 
idea...

Thanks for all solutions...

Merci & Gruss
Georg

-- 
Georg Hoermann, Dep. of Hydrology, Ecology, Kiel University, Germany
Tel. 0431-880-1207, icq - 348 340 729, 0172/4315715, Penguin #189476




More information about the R-help mailing list