2

正如您在下面看到的,a 是一个空的“xts”对象。不知何故,当用日期索引时,它会以字符串形式返回暗名......

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

1 回答 1

2

当我们尝试 susbset 索引时,这似乎是 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 ""
于 2013-01-15T16:48:45.833 回答