R xts 和 data.table
我可以将 data.table 转换为 xts 对象,就像处理 data.frame 一样:
> df = data.frame(x = c("a", "b", "c", "d"), v = rnorm(4))
> dt = data.table(x = c("a", "b", "c", "d"), v = rnorm(4))
> xts(df, as.POSIXlt(c("2011-01-01 15:30:00", "2011-01-02 15:30:00", "2011-01-03 15:50:50", "2011-01-04 15:30:00")))
x v
2011-01-01 15:30:00 "a" "-1.2232283"
2011-01-02 15:30:00 "b" "-0.1654551"
2011-01-03 15:50:50 "c" "-0.4456202"
2011-01-04 15:30:00 "d" "-0.9416562"
> xts(dt, as.POSIXlt(c("2011-01-01 15:30:00", "2011-01-02 15:30:00", "2011-01-03 15:50:50", "2011-01-04 15:30:00")))
x v
2011-01-01 15:30:00 "a" " 1.3089579"
2011-01-02 15:30:00 "b" "-1.7681071"
2011-01-03 15:50:50 "c" "-1.4375100"
2011-01-04 15:30:00 "d" "-0.2467274"
将 data.table 与 xts 一起使用是否存在任何问题?
I can convert a data.table to an xts object just as I do with a data.frame:
> df = data.frame(x = c("a", "b", "c", "d"), v = rnorm(4))
> dt = data.table(x = c("a", "b", "c", "d"), v = rnorm(4))
> xts(df, as.POSIXlt(c("2011-01-01 15:30:00", "2011-01-02 15:30:00", "2011-01-03 15:50:50", "2011-01-04 15:30:00")))
x v
2011-01-01 15:30:00 "a" "-1.2232283"
2011-01-02 15:30:00 "b" "-0.1654551"
2011-01-03 15:50:50 "c" "-0.4456202"
2011-01-04 15:30:00 "d" "-0.9416562"
> xts(dt, as.POSIXlt(c("2011-01-01 15:30:00", "2011-01-02 15:30:00", "2011-01-03 15:50:50", "2011-01-04 15:30:00")))
x v
2011-01-01 15:30:00 "a" " 1.3089579"
2011-01-02 15:30:00 "b" "-1.7681071"
2011-01-03 15:50:50 "c" "-1.4375100"
2011-01-04 15:30:00 "d" "-0.2467274"
Is there any issue in using data.table with xts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只是为了解决一个悬而未决的问题。
正如文森特在评论中指出的那样,这没有问题。
它包含在 data.table 1.9.5 中。下面是类似的内容:
Just to resolve an open question.
As Vincent point in the comment there is no issue about that.
It is included in data.table 1.9.5. Below is the similar content:
由于 quantmod,通常有
xts
符号嵌入在所有列名称中。 (例如“SPY.Open”、“SPY.High”等)。因此,这是 Jan 的as.data.table.xts
的替代方案,它将符号放在单独的列中,这在data.table
中更自然(因为您在进行任何分析之前,可能会重新绑定其中的一些)。Because of quantmod, it is common to have an
xts
with the symbol embedded in all the column names. (e.g. "SPY.Open", "SPY.High", etc.). So, here is an alternative to Jan'sas.data.table.xts
that puts the symbol in a separate column, which is more natural indata.table
s (since you're probably going to rbind a bunch of these before doing any analysis).