我正在手动加载一些数据(与通过 quantmod 相比)并尝试创建一个 xts 类(似乎一切正常),但我似乎无法使用 Date 类型索引。
我试图找到与另一个时间序列(NDX,通过 quantmod)的交集,如果我可以使用日期索引,这应该很容易。
我假设我在创建 xts 对象时遗漏了一些东西,但不知道是什么。## 标题 ##
VXN.tmp <- read.csv('VXNDailyPrices.csv', na.strings=c('n/a'), stringsAsFactors=F, header=F)
VXN <- xts(as.matrix(VXN.tmp[,-1]), as.Date(VXN.tmp[,1], tz="", format='%m/%d/%y'), tzone="")
colnames(VXN) <- c('Open', 'High', 'Low', 'Close')
VXN <- VXN[!is.na(Cl(VXN))]
> head(VXN)
Open High Low Close
2001-02-02 NA NA NA 54.89
2001-02-05 NA NA NA 55.85
2001-02-06 NA NA NA 53.68
2001-02-07 NA NA NA 54.41
2001-02-08 NA NA NA 54.66
2001-02-09 NA NA NA 55.85
> VXN['2001-02-02']
Open High Low Close
2001-02-02 NA NA NA 54.89
> VXN[as.Date('2001-02-02')]
Open High Low Close
>
其他一些信息
> str(VXN)
An ‘xts’ object from 2001-02-02 to 2013-03-01 containing:
Data: num [1:3040, 1:4] NA NA NA NA NA NA NA NA NA NA ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:4] "Open" "High" "Low" "Close"
Indexed by objects of class: [Date] TZ:
xts Attributes:
List of 2
$ tclass: chr "Date"
$ tzone : chr ""
>
> vxnidx <- head(index(VXN), 20)
> dput(vxnidx)
structure(c(11355, 11358, 11359, 11360, 11361, 11362, 11365,
11366, 11367, 11368, 11369, 11373, 11374, 11375, 11376, 11379,
11380, 11381, 11382, 11383), class = "Date")
>
> vxnidx[1]
[1] "2001-02-02"
> VXN[vxnidx[1]]
Open High Low Close
> VXN["2001-02-02"]
Open High Low Close
2001-02-02 NA NA NA 54.89
>
数据来自此处的 CBOE:http: //www.cboe.com/micro/vxn/#historical
2/2/2001,n/a,n/a,n/a,54.89
2/5/2001,n/a,n/a,n/a,55.85
2/6/2001,n/a,n/a,n/a,53.68
2/7/2001,n/a,n/a,n/a,54.41
2/8/2001,n/a,n/a,n/a,54.66
2/9/2001,n/a,n/a,n/a,55.85
2/12/2001,n/a,n/a,n/a,57.05
2/13/2001,n/a,n/a,n/a,58.42
2/14/2001,n/a,n/a,n/a,57.49
2/15/2001,n/a,n/a,n/a,55.19
2/16/2001,n/a,n/a,n/a,55.34
2/20/2001,n/a,n/a,n/a,57.93
2/21/2001,n/a,n/a,n/a,59.57
2/22/2001,n/a,n/a,n/a,60.99
2/23/2001,n/a,n/a,n/a,62.03
2/26/2001,n/a,n/a,n/a,61.77
2/27/2001,n/a,n/a,n/a,63.27
2/28/2001,n/a,n/a,n/a,63.65
3/1/2001,n/a,n/a,n/a,64.32
编辑:会话信息
> sessionInfo()
R version 2.15.2 (2012-10-26)
Platform: i386-apple-darwin9.8.0/i386 (32-bit)
locale:
[1] en_AU.UTF-8/en_AU.UTF-8/en_AU.UTF-8/C/en_AU.UTF-8/en_AU.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] PerformanceAnalytics_1.0.4.4 quantmod_0.3-17 TTR_0.21-1
[4] xts_0.8-6 zoo_1.7-7 Defaults_1.1-1
loaded via a namespace (and not attached):
[1] grid_2.15.2 lattice_0.20-10
>