【发布时间】:2015-01-12 03:22:24
【问题描述】:
我在访问 IBrokers 包中的时间戳数据时遇到了一些问题。
这是我得到的数据示例:
AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.WAP AAPL.hasGaps AAPL.Count
2015-01-09 17:59:00 112 112.04 111.95 112 6043 112.011 0 2240
所以当我运行data[,0] 时,我得到了
2015-01-09 17:59:00
问题是,稍后当我尝试将其保存到 MySQL 表中时,我收到以下错误:
Error in dimnames(cd) <- list(as.character(index(x)), colnames(x)) :
'dimnames' applied to non-array
看起来data[,0] 不只是包含时间戳。
当我对包含 data[,0] 的变量 ts 进行摘要时,我得到:
Error in `colnames<-`(`*tmp*`, value = c("ts.1", "ts.0")) :
'names' attribute [2] must be the same length as the vector [1]
任何关于如何访问时间戳或将ts 的内容转换为字符以便我可以将其插入数据库的提示将不胜感激。
编辑:
dput() 输出
structure(c(112, 112.04, 111.95, 112, 6043, 112.011, 0, 2240), .Dim = c(1L,
8L), index = structure(1420837140, tzone = "", tclass = c("POSIXct",
"POSIXt")), .indexCLASS = c("POSIXct", "POSIXt"), tclass = c("POSIXct",
"POSIXt"), .indexTZ = "", tzone = "", .Dimnames = list(NULL,
c("AAPL.Open", "AAPL.High", "AAPL.Low", "AAPL.Close", "AAPL.Volume",
"AAPL.WAP", "AAPL.hasGaps", "AAPL.Count")), class = c("xts",
"zoo"), from = "20150112 02:52:24", to = "20150112 02:53:24", src = "IB", updated = structure(33434342.12435, class = c("POSIXct",
"POSIXt")))
【问题讨论】:
-
原始
AAPL数据的结构是什么?你能在上面运行dput()或str()吗?我从未见过data[,0]索引,但它似乎返回了一个带有行名的空矩阵——你最好只做rownames(data)来获取时间戳。 -
为什么用 0 作为子集?你知道 R 使用基于 1 的索引,对吗?
-
@JoshuaUlrich 这是我发现访问时间戳的唯一方法。如果我做了 data[,1] 那么我得到
AAPL.Open。但我正在学习 R,所以我的方法可能是错误的,它只是返回我时间戳的唯一方法。任何有关如何做到这一点的提示将不胜感激。 -
阅读动物园和 xts 小插曲。
-
@JoshuaUlrich 谢谢,动物园做到了。 time(ts) 是我的答案。感谢您的提示。