[R] Using 'diff' on zoo vs zooreg classes (possible bug?)

Gabor Grothendieck ggrothendieck at gmail.com
Thu Aug 2 11:58:45 CEST 2007

On 8/2/07, Josh Quigley <josh.quigley at tibra.com.au> wrote:
> Hello,
> Can anyone explain the following behaviour? To me it seems a bug, but maybe
> it is intentional.
> It seems that a diff on a zooreg class that is not _strictly_ regular only
> considers those entries that are 'deltat' apart.
> In the following, diff on the zooreg class only returns values where the
> index was one second apart. The example replicates by dev code, but I've
> also tested using plain old integer indices, and the behaviour is the same.

Correct, that's how it is intended to work.  diff(zr) is the same as
zr - lag(zr, -1) and lag.zoo and lag.zooreg work differently.  For zoo
objects the lagged version is obtained by moving the time points
to the adjacent time point that exists in the series but for zooreg objects
lag the time is lagged by deltat.

A key difference is that in zooreg one can lag a point to a time point
that did not previously exist in the series and, in particular, can lag
a series outside of the original time range.  That can be important
when performing dynamic regression, i.e. regression on lagged
and differenced versions of the series, and is discussed briefly in:


In contrast to zooreg, the time points of a lagged zoo series will only be
chosen from the time points of the original series.

You can choose to make your object a zoo object rather than a
zooreg object if you want the zoo rather than zooreg behavior,

Also note that diff.zooreg has an na.pad= argument that may be
what you are looking for:

> diff(zr, na.pad = TRUE)
                       V3    V4      V5     V6
2007-01-10 11:14:38    NA    NA      NA     NA
2007-01-10 11:14:39  0.00  0.00 0.00000 -2e-05
2007-01-10 11:14:40  0.00 -0.01 0.00000  0e+00
2007-01-10 11:14:43    NA    NA      NA     NA
2007-01-10 11:14:45    NA    NA      NA     NA
2007-01-10 11:14:46  0.00  0.00 0.00000  0e+00
2007-01-10 11:14:49    NA    NA      NA     NA
2007-01-10 11:14:50  0.00  0.00 0.00782  0e+00
2007-01-10 11:14:51  0.00  0.03 0.00000  0e+00
2007-01-10 11:14:52 -0.01  0.01 0.00000  0e+00
2007-01-10 11:15:00    NA    NA      NA     NA

More information about the R-help mailing list