R中的空xts对象看起来有问题

时间:2013-01-15 15:30:22

标签: r xts

如下所示,a是一个空的'xts'对象。不知何故,在使用日期编制索引时,它会将dimname作为字符串返回...

> a
Split
> dimnames(a)
[[1]]
NULL
[[2]]
[1] "Split"
> str(a)
An 'xts' object of zero-width
> str(a['2003'])
chr "Split"

有没有人遇到过这个?有没有人知道解决方法? 任何帮助表示赞赏。

以下是实际情况

library(TTR)
d = getYahooData('ACE', 20121201, 20121231, freq="daily", type="split", adjust=F, quiet=T)
s = d[!is.na(d[, 2]), 2]
> s
Split
> s['2012']
[1] "Split"
> str(s)
An 'xts' object of zero-width
> str(s['2012'])
chr "Split"

> sessionInfo()
R version 2.14.1 (2011-12-22)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8       
[4] LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
[7] LC_PAPER=C                 LC_NAME=C                  LC_ADDRESS=C              
[10] LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
  [1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
  [1] XML_3.95-0.1    tseries_0.10-30 TTR_0.21-1      xts_0.9-1       zoo_1.7-9      

loaded via a namespace (and not attached):
  [1] grid_2.14.1      lattice_0.20-0   quadprog_1.5-4   rstudio_0.97.248 tools_2.14.1    

1 个答案:

答案 0 :(得分:2)

当我们尝试设置索引时,它似乎是xts对象的错误。

一种解决方法是使用drop = TRUE

d = getYahooData('ACE', 20121201, 20121231, freq="daily", type="split", adjust=F, quiet=T)
s = d[!is.na(d[, 2]), 2,drop=TRUE]

 str(s)
An 'xts' object of zero-width

str(s['2012'])
An ‘xts’ object on NA/NA containing:
  Data: logi [1, 1] NA
  Indexed by objects of class: [POSIXct,POSIXt] TZ: 
  xts Attributes:  
List of 2
 $ tclass: chr [1:2] "POSIXct" "POSIXt"
 $ tzone : chr ""