[R] timeseries aggregation by event (zoo, hydrostat, plyr ?)
Bastian Pöschl
bstan0578 at gmail.com
Fri Jan 30 11:52:46 CET 2015
hallo, i search for a possibillity to aggregate a time series by non
regular periods (events)
As an example i have a precipitation time series
## generate some smooth timeseries
library(zoo)
library(hydrostats)
## some example data
## ## ## ##
x.pr <- c(0, 0, 0, 0.2, 0.8, 0.7, 0, 0, 0, 0.6, 0.9, 0.9, 1, 0, 0, 0, 0.08,
0.01, 0.09, 0, 0, 0, 0)
z.pr <- zooreg(x.pr, start=as.POSIXct("2013-01-01 00:00:20"),
frequency=0.00005)
zt <- zooreg(rnorm(20000), start=as.POSIXct("2013-01-01
00:00:01"),frequency=0.05)
z<-merge(zt, z.pr, all = TRUE)
Zf<-na.spline(z[,2], na.rm = FALSE)
Zf[Zf<=0.001]<-0
## ## ## ##
#plot(Zf)
## i can get the rain events easily with hydrostats package
P.df<-data.frame(Date=time(Zf),Q=coredata(Zf)) ##data.frame as taken by
hydrostats
P.ev<-high.spell.lengths(P.df, threshold=0.1, ind.days = 0) ##spell.length
will be in 20 secs
P.ev$end.date<-P.ev$start.date+P.ev$spell.length*20
## now i want to aggregate data within this events
## i need to get min/max/mean value
## by now i use window.zoo within a for loop like
for (i in 1:nrow(P.ev)){P.ev$Max[i]<-max((window(P.ev,
start=P.ev$start.date[i], end=P.ev$end.date[i])),na.rm=TRUE)}
## but this takes a lot of time within a dacade series
## any suggestions how i might speed this up ?
## i tried aggregate.zoo, ddply but got no simple solution for this...
## hydrostats seems to do something like it within the high.spells function
but the output is just an overall statistic
Thank you dear HelpRs
[[alternative HTML version deleted]]
More information about the R-help
mailing list