[R] How to average time series data within certain time periods

Dominik van Pinxteren dominik at tropos.de
Mon Jun 18 17:10:21 CEST 2012


Hi,

I am new to R and wondering how I can subset and average time series data  
based on given irregular time periods:

Say I have a time series of a measured variable "x" (hourly data) in  
"values.csv" with columns "date" and "x" which I would like to average  
within certain irregular time periods ("events"), given in "events.csv"  
with columns "EventID", "start", and "stop" (where start and stop indicate  
the start and stop dates of the corresponding event).

What I usually do is to load the two csvs into a database and then perform  
an SQL statement like this:

SELECT a.EventID, avg(b.x) from events as a, values as b WHERE b.date  
between a.start and a.stop GROUP BY a.EventID

This gives a table with the EventIDs and the corresponding mean values of  
x as an output.

As my data are not that large I would like to avoid the database and do  
the averaging in R but I cannot find the way of doing this in R.

I would appreciate any advice to set me on track.

Thanks,
Dominik



More information about the R-help mailing list