问题
I am using R 3.2.1 for Mac OS X and seem to have run into incorrect behavior in xts subsetting. In brief, subsetting monthly data give a result that is 1 month lagged from what it should be. Here is a simple example that is similar to an analysis of paleotemperature I am doing:
First I will make some test data for the example:
xts.test <- xts(rnorm(440*12, mean=0, sd=10),order.by=timeBasedSeq(155001/1989))
This produces a correct xts
file AFAICT. Here is the first year of 12 months.
head(xts.test, 12L)
[,1]
Jan 1550 -6.9301845
Feb 1550 12.1581413
Mar 1550 3.9688139
Apr 1550 3.9540268
May 1550 9.8200923
Jun 1550 -4.2090998
Jul 1550 7.5950340
Aug 1550 -6.5967389
Sep 1550 -0.6736532
Oct 1550 6.4939221
Nov 1550 4.3916465
Dec 1550 19.8800872
However, when I try to subset this by selecting for a single year, I get the following:
xts.test["1550"]
[,1]
Feb 1550 12.1581413
Mar 1550 3.9688139
Apr 1550 3.9540268
May 1550 9.8200923
Jun 1550 -4.2090998
Jul 1550 7.5950340
Aug 1550 -6.5967389
Sep 1550 -0.6736532
Oct 1550 6.4939221
Nov 1550 4.3916465
Dec 1550 19.8800872
Jan 1551 -2.9549224
That is, instead of the correct Jan-Dec 1550, I get Feb 1550 through Jan 1551
I get a similar lag when I try to subset by selecting months of a year. c(2, 3,4,5,6,7)
get me April through September instead of March through August.
Any thoughts here?
回答1:
This is a bug that has been fixed in the most recent development version on GitHub.
R> require(xts)
R> xts.test <- xts(rnorm(440*12, mean=0, sd=10),order.by=timeBasedSeq(155001/1989))
R> packageVersion("xts")
[1] ‘0.9.7’
R> str(xts.test) # notice TZ is not set
An ‘xts’ object on Jan 1550/Dec 1989 containing:
Data: num [1:5280, 1] -8.11 -7.65 2.07 -3.29 17.03 ...
Indexed by objects of class: [yearmon] TZ:
xts Attributes:
NULL
Notice that TZ
is not set above, but it's set in the new version below.
R> packageVersion("xts")
[1] ‘0.9.8’
R> str(xts.test) # notice TZ is set
An ‘xts’ object on Jan 1550/Dec 1989 containing:
Data: num [1:5280, 1] 0.357 12.318 24.291 22.181 6.123 ...
Indexed by objects of class: [yearmon] TZ: UTC
xts Attributes:
NULL
来源:https://stackoverflow.com/questions/31777724/xts-subsetting-gives-incorrect-results-for-months