Mathieu Beaulieu
2009-Aug-18 20:45 UTC
[R] aggregating values at discreet irregular time intervals into hourly values
Hello R users, I'm a newby to R (and programming software at large) and I would need some help to sum up event data at discreet time and irregular time interval into a hourly frequency. Here is an example of my time series frame (irregular time-serie object - irts in the tseries package): time value 2008-12-19 19:11:03 GMT 1 2008-12-19 19:12:00 GMT 0 2008-12-19 19:42:39 GMT 1 2008-12-19 20:42:41 GMT 1 2008-12-19 20:42:45 GMT 1 2008-12-19 20:42:48 GMT 1 2008-12-19 20:42:55 GMT 1 2008-12-19 20:42:57 GMT 1 2008-12-19 20:43:07 GMT 1 2008-12-19 20:43:16 GMT 1 2008-12-19 21:05:45 GMT 1 2008-12-19 21:07:58 GMT 1 2008-12-19 22:08:00 GMT 1 2008-12-19 22:08:33 GMT 1 2008-12-19 23:09:58 GMT 1 2008-12-19 23:10:32 GMT 1 2008-12-19 23:13:00 GMT 0 2008-12-19 23:14:29 GMT 1 2008-12-19 23:14:32 GMT 1 2008-12-19 23:14:35 GMT 1 2008-12-19 23:30:35 GMT 1 2008-12-19 23:38:56 GMT 1 2008-12-19 23:44:00 GMT 0 2008-12-19 23:45:00 GMT 0 2008-12-20 00:46:00 GMT 1 [...] 2009-05-13 13:45:36 GMT 1 And i'm trying to obtain a regularly spaced time series object which would aggregates the events (value ==1) occuring 30 minutes before and after every hour into a hourly rate, a little bite like this: time value 2008-12-19 18:00:00 GMT 0 2008-12-19 19:00:00 GMT 1 2008-12-19 20:00:00 GMT 1 2008-12-19 21:00:00 GMT 9 2008-12-19 22:00:00 GMT 2 2008-12-19 23:00:00 GMT 5 2008-12-20 00:00:00 GMT 2 2008-12-20 01:00:00 GMT 1 [...] 2009-05-13 13:00:00 GMT 0 2009-05-13 14:00:00 GMT 1 So far, I've created a regular ts object (destination object I guess) based on my irregular one: Code example (not to be run): test.ts<-approx.irts(test.irts,seq(from=test.irts$time[1], to=test.irts$time[length(test.irts$time)],by = "1 hour"),rule=2,method="linear") Which creates the regular ts fine, but doesn't aggregate the events. I've tried to mess around with the aggregate function - but I can't get the codes right to do what I want. Here are examples of unsuccessful attempts: try = aggregate(test.irts$value, by= list(time=test.ts$time), FUN= mean) ERROR MESS: Error in FUN(X[[1L]], ...) : arguments must have same length try = aggregate.ts(test.irts$value,frequency(test.ts$time),nfrequency=1, ndeltat=1) PROBLEM: gives me only one vector for my values (no time vector) but they are NOT aggregated by hour. try = list(x=test.ts$time, y=aggregate.ts(test.irts$value, nfrequency=1)) PROBLEM: Gives 2 separate vectors :hourly time step and values - but again, values are not aggregated by hour. I would appreciate any cue or help. Thank you Mathieu Beaulieu, Soil, Water & Environment Laboratory Institute for Resources, Environment and Sustainability University of British-Columbia 429-2202 Main Mall, Vancouver, BC, V6T 1Z4 Canada landfood.ubc.ca/swel _________________________________________________________________ Internet explorer 8 aide à protéger la vie privée. [[alternative HTML version deleted]]
Gabor Grothendieck
2009-Aug-18 21:24 UTC
[R] aggregating values at discreet irregular time intervals into hourly values
Try this: library(tseries) # create 3 hours of test data set.seed(123) st <- as.POSIXct("2008-12-19 19:11:03") tt <- seq(from = st, to = st + 60 * 60 * 3, by = "min") x <- irts(tt, sample(0:1, length(tt), replace = TRUE)) library(zoo) # convert to zoo and chron z <- zoo(coredata(x), as.chron(format(time(x)))) # add half hour to each time and then truncate to the hour # aggregating over that z.ag <- aggregate(z, trunc(time(z) + as.numeric(times("00:30:00")), "01:00:00"), sum) z.ag which gives:> z.ag(12/19/08 19:00:00) (12/19/08 20:00:00) (12/19/08 21:00:00) (12/19/08 22:00:00) 10 28 32 17 Read the three vignettes (pdf documents) that come with zoo, see ?aggregate.zoo in particular and see R News 4/1 for dates and times. On Tue, Aug 18, 2009 at 4:45 PM, Mathieu Beaulieu<beam09 at hotmail.com> wrote:> > Hello R users, > > I'm a newby to R (and programming software at large) and I would need some help to sum up event data at discreet time and irregular time interval into a hourly frequency. > > Here is an example of my time series frame (irregular time-serie object - irts in the tseries package): > > time ? ? ? ? ? ? ? ? ? ? ? ?value > 2008-12-19 19:11:03 GMT ? ?1 > 2008-12-19 19:12:00 GMT ? ? 0 > 2008-12-19 19:42:39 GMT ? ? 1 > 2008-12-19 20:42:41 GMT ? ? 1 > 2008-12-19 20:42:45 GMT ? ? 1 > 2008-12-19 20:42:48 GMT ? ? 1 > 2008-12-19 20:42:55 GMT ? ? 1 > 2008-12-19 20:42:57 GMT ? ? 1 > 2008-12-19 20:43:07 GMT ? ? 1 > 2008-12-19 20:43:16 GMT ? ? 1 > 2008-12-19 21:05:45 GMT ? ? 1 > 2008-12-19 21:07:58 GMT ? ? 1 > 2008-12-19 22:08:00 GMT ? ? 1 > 2008-12-19 22:08:33 GMT ? ? 1 > 2008-12-19 23:09:58 GMT ? ? 1 > 2008-12-19 23:10:32 GMT ? ? 1 > 2008-12-19 23:13:00 GMT ? ? 0 > 2008-12-19 23:14:29 GMT ? ? 1 > 2008-12-19 23:14:32 GMT ? ? 1 > 2008-12-19 23:14:35 GMT ? ? 1 > 2008-12-19 23:30:35 GMT ? ? 1 > 2008-12-19 23:38:56 GMT ? ? 1 > 2008-12-19 23:44:00 GMT ? ? 0 > 2008-12-19 23:45:00 GMT ? ? 0 > 2008-12-20 00:46:00 GMT ? ? 1 > [...] > 2009-05-13 13:45:36 GMT ? ?1 > > And i'm trying to obtain a regularly spaced time series object which would aggregates the events (value ==1) occuring 30 minutes before and after every hour into a hourly rate, a little bite like this: > > time ? ? ? ? ? ? ? ? ? ? ? ?value > 2008-12-19 18:00:00 GMT ? ?0 > 2008-12-19 19:00:00 GMT ? ? 1 > 2008-12-19 20:00:00 GMT ? ? 1 > 2008-12-19 21:00:00 GMT ? ? 9 > 2008-12-19 22:00:00 GMT ? ? 2 > 2008-12-19 23:00:00 GMT ? ? 5 > 2008-12-20 00:00:00 GMT ? ? 2 > 2008-12-20 01:00:00 GMT ? ?1 > [...] > 2009-05-13 13:00:00 GMT ? ?0 > 2009-05-13 14:00:00 GMT ? ?1 > > So far, I've created a regular ts object (destination object I guess) based on my irregular one: > > Code example (not to be run): > test.ts<-approx.irts(test.irts,seq(from=test.irts$time[1], > to=test.irts$time[length(test.irts$time)],by = "1 hour"),rule=2,method="linear") > > Which creates the regular ts fine, but doesn't aggregate the events. > > I've tried to mess around with the aggregate function - but I can't get the codes right to do what I want. Here are examples of unsuccessful attempts: > > try = aggregate(test.irts$value, by= list(time=test.ts$time), FUN= mean) > ERROR MESS: Error in FUN(X[[1L]], ...) : arguments must have same length > > try = aggregate.ts(test.irts$value,frequency(test.ts$time),nfrequency=1, ndeltat=1) > PROBLEM: gives me only one vector for my values (no time vector) but they are NOT aggregated by hour. > > try = list(x=test.ts$time, y=aggregate.ts(test.irts$value, nfrequency=1)) > PROBLEM: Gives 2 separate vectors :hourly time step and values - but again, values are not aggregated by hour. > > > I would appreciate any cue or help. Thank you > > > > > Mathieu Beaulieu, > Soil, Water & Environment Laboratory > Institute for Resources, Environment and Sustainability > University of British-Columbia > 429-2202 Main Mall, > Vancouver, BC, > V6T 1Z4 > Canada > landfood.ubc.ca/swel > > > _________________________________________________________________ > Internet explorer 8 aide ? prot?ger la vie priv?e. > > ? ? ? ?[[alternative HTML version deleted]] > > > ______________________________________________ > R-help at r-project.org mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >