【问题标题】:Saving timestamp on IBrokers package在 IBrokers 包上保存时间戳
【发布时间】: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) 是我的答案。感谢您的提示。

标签: mysql r ibrokers


【解决方案1】:

正如@JoshuaUlrich 对我的问题的cmets 所建议的那样,答案在zoo 包上。

关于动物园的完整文档可以在here找到

在我的特殊情况下,通过包含 zoo 库并简单地做:

time(data[,0])

我解决了Error in dimnames() 错误。

希望对其他人有所帮助。

【讨论】:

  • 根本不需要索引。只需time(data) 即可。
猜你喜欢
  • 2015-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-03
  • 2021-12-31
  • 2017-10-05
  • 1970-01-01
相关资源
最近更新 更多