Re: [Rd] [R] aggregate.ts

From: Paul Gilbert <>
Date: Wed, 25 Jul 2007 10:31:06 -0400

(moved from r-help)

Achim Zeileis wrote:

>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.

I've been caught by this before, and complained before. It does not do what most people that work with economic time series would expect. (One might argue that not all time series are economic, but other time series don't usually fit with ts very well.) At the very least aggregate should issue a warning. Quarterly observations are for quarters of the year, so just arbitrarily grouping in 3 beginning with the first observation is *extremely* misleading, even if it is documented.

[ BTW, there is a bug in the print method here (R-2.5.1 on Linux) :

 >   y2 <- aggregate(x2, nfreq = 4) 
 > y2

Error in"", start.pad) : invalid number of copies in  > traceback()
5:"", start.pad)
4: as.vector(data)
3: matrix(c("", start.pad), format(x, ...),"",
       end.pad)), nc = fr.x, byrow = TRUE, dimnames = list(dn1,

2: print.ts(c(6L, 15L, 24L, 33L, 42L, 51L, 60L, 69L)) 1: print(c(6L, 15L, 24L, 33L, 42L, 51L, 60L, 69L)) ]


>Currently, the "zoo" implementation allows this: Coercing back and forth
> library("zoo")
> z1 <- as.ts(aggregate(as.zoo(x1), as.yearqtr, sum))
> z2 <- as.ts(aggregate(as.zoo(x2), as.yearqtr, sum))

This is better, but still potentially misleading. I would prefer a default NA when only some of the observations are available for a quarter (and the syntax is a bit cumbersome for something one needs to do fairly often).


>where z1 is identical to y1, and z2 is what you probably want.
> mailing list
>PLEASE do read the posting guide
>and provide commented, minimal, self-contained, reproducible code.

La version franšaise suit le texte anglais.

This email may contain privileged and/or confidential inform...{{dropped}} mailing list Received on Wed 25 Jul 2007 - 15:07:18 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 26 Jul 2007 - 16:36:41 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.