[R] xts off by one confusion or error

Tim Coote tim+r-project.org at coote.org
Thu Apr 8 16:53:02 CEST 2010


Hullo
I may have missed something blindingly obvious here. I'm using xts to  
handle some timeseries data. I've got daily measurements for 100  
years. If I try to reduce the error rate by taking means of each  
month, I'm getting what at first sight appears to be conflicting  
information. Here's a small subset to show the problem:

A small set of data:
 > vv
              x
2010-02-01 6.1
2010-02-02 6.1
2010-02-03 6.0
2010-02-04 6.0
2010-02-05 6.0
2010-02-06 6.1
2010-02-07 6.1
2010-02-08 6.1
2010-02-09 6.1
2010-02-10 6.2

Aggregate:
 > aggregate (vv, as.yearmon (index (vv)), mean)

Feb 2010 6.08

That's fine. But if I explicitly convert to xts (which the answer  
ought to be, so this should be a noop), the values shift back by one  
month:
 > xts (aggregate (vv, as.yearmon (index (vv)), mean))
             x
Jan 2010 6.08

Just to confirm the classes:
 > class (aggregate (vv, as.yearmon (index (vv)), mean))
[1] "zoo"

 > class (vv)
[1] "xts" "zoo"

And to confirm that as.yearmon is returning the right month:
 > as.yearmon (index (vv))
  [1] "Feb 2010" "Feb 2010" "Feb 2010" "Feb 2010" "Feb 2010" "Feb 2010"
  [7] "Feb 2010" "Feb 2010" "Feb 2010" "Feb 2010"

This run was on a stock Fedora 10 build:
 > version
                _
platform       i386-redhat-linux-gnu
arch           i386
os             linux-gnu
system         i386, linux-gnu
status
major          2
minor          10.0
year           2009
month          10
day            26
svn rev        50208
language       R
version.string R version 2.10.0 (2009-10-26)

And from installed.packages ():
xts        NA               NA      "GPL-3"                "2.10.0"
zoo        NA               NA      "GPL-2"                "2.10.0"

Any help gratefully received.

Tim



More information about the R-help mailing list