[R] aggregate.ts

Achim Zeileis Achim.Zeileis at wu-wien.ac.at
Wed Jul 25 03:06:12 CEST 2007

On Wed, 25 Jul 2007, laimonis wrote:

> Consider the following scrap of code:

...slightly modified to
  x1 <- ts(1:24, start = c(2000, 10), freq = 12)
  x2 <- ts(1:24, start = c(2000, 11), freq = 12)

and then
  y1 <- aggregate(x1, nfreq = 4)
gives the desired result while
  y2 <- aggregate(x2, nfreq = 4)
probably does not what you would like it to do. In both cases, the 24
observations are broken into 8 segments of equal length (as documented on
?aggregate.ts) and then aggregated. Therefore
are identical (and not matched by quarter...as you would probably like).

> And don't tell me that the aggregating a monthly series into quarters
> makes no sense!! (see response to Bug 9798).

1. Your tone is not appropriate.
2. You're not quoting the reply correctly. It said: "You cannot aggregate
   a time series that does not run over quarters into quarters. The
   speculation is plain wrong."
   The reply means that aggregate.ts() does not do what you think
   it does. As I tried to illustrate with the example above.

One can probably argue about whether it makes sense to aggregate a monthly
time series into quarter when I don't have complete observations in each
quarter. But maybe it might be worth considering a change in
aggregate.ts() so that the old and new frequency are matched even with
incomplete observations?

Currently, the "zoo" implementation allows this: Coercing back and forth
  z1 <- as.ts(aggregate(as.zoo(x1), as.yearqtr, sum))
  z2 <- as.ts(aggregate(as.zoo(x2), as.yearqtr, sum))
where z1 is identical to y1, and z2 is what you probably want.


More information about the R-help mailing list