[R] Aggregating frequency of irregular time series

Gabor Grothendieck ggrothendieck at myway.com
Fri Mar 26 05:54:51 CET 2004



Assume r.its is an its time series and assume that chron has been loaded.

chron represents dates as days since an origin so the week number can be
obtained by dividing by 7.  Then tapply the mean or other function.
Finally, recreate an its.  

week <- 7 * (as.numeric( chron( format( r.its at dates, "%m/%d/%Y" ) ) ) %/% 7 )
z <- tapply( r.its, week, mean )
z <- its( c(z), date = as.POSIXct( chron( as.numeric( names( z ) ) ) ) )

There is a new Dates class in R 1.9.0 that could be used in place of chron.


Ivan Alves <papucho <at> mac.com> writes:

: 
: Thank you very much Brian.  Indeed, by looking at ?tapply() this would 
: do the job for regular time series (whose data-time INDEX renders 
: itself naturally for " bundling data" in regular groups). However, with 
: irregular time series the story is different, as some careful 
: "bundling" is necessary prior to applying tapply(). Whereas this may 
: not be a problem for months (creating strings month-year), it would be 
: for weeks, for instance.  Also the object returned would need to be 
: converted again to a time series object with additional function calls. 
: Furthermore, the function AggregateSeries() provides additional 
: functionality, such as creating moving averages, rolling variances, 
: minima and maxima, all with options to the same function call. I take 
: from your response that there is no easy way out (an already existing 
: function), and that some programming will be required.
: 
: Kind regards,
: 
: Ivan
: _______________________
: Ivan Alves
: mailto://papucho <at> mac.com
: On 25 Mar 2004, at 22:51, Prof Brian Ripley wrote:
: 
: > R itself has no support for irregular time series, but it does have an
: > aggregate method for regular ones.  You need to look into whichever
: > package is handling irregular time series.  However, it seems to me 
: > that
: > this is not a time series problem at all: you have a set of 
: > observations
: > whose indices are data-times, and tapply() will do the job.
: >
: > On Thu, 25 Mar 2004, Ivan Alves wrote:
: >
: >> S-Plus has the function AggregateSeries() whose name is self
: >> explanatory.  For instance one can derive monthly series from daily
: >> ones by specifying end-of-period, averages, sums, etc.  I looked for a
: >> similar function in the packages "its" and "tseries", but found
: >> nothing.  I also help.searched() for aggregate to no avail.  Would
: >> anybody be so kind to point me in the right direction?
: >
: > -- 
: > Brian D. Ripley,                  ripley <at> stats.ox.ac.uk
: > Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
: > University of Oxford,             Tel:  +44 1865 272861 (self)
: > 1 South Parks Road,                     +44 1865 272866 (PA)
: > Oxford OX1 3TG, UK                Fax:  +44 1865 272595
: >
: 
: ______________________________________________
: R-help <at> stat.math.ethz.ch mailing list
: https://www.stat.math.ethz.ch/mailman/listinfo/r-help
: PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
: 
:




More information about the R-help mailing list