【问题标题】:Converting xts to data.table in R -issue with the setkeyv argument使用 setkeyv 参数在 R 问题中将 xts 转换为 data.table
【发布时间】:2021-08-05 15:55:09
【问题描述】:

我有xts格式的数据如下:

                     EURAUD  EURCAD  EURCHF
2019-03-05 00:00:00 1.59898 1.50877 1.13258
2019-03-05 00:01:00 1.59893 1.50870 1.13252
2019-03-05 00:02:00 1.59888 1.50888 1.13253

由于不理解 setkeyv 参数,我需要一些帮助来将 xts 转换为 data.table

structure(c(1.59898, 1.59893, 1.59888, 1.59886, 1.59894, 1.50877, 1.5087, 1.50888, 1.50879, 1.50885, 
1.13258, 1.13252, 1.13253, 1.13254, 1.13249), class = c("xts", "zoo"), index = 
structure(c(1551733200, 1551733260, 1551733320, 1551733380, 1551733440), tzone = "", tclass = 
c("POSIXct", "POSIXt")), .Dim = c(5L, 3L), .Dimnames = list(NULL, c("EURAUD", "EURCAD", "EURCHF")))

EDIT 在其他函数中使用data.table(从xts 转换为data.table 后)时出现以下错误。

> library(highfrequency)
> rc = rCov(rData = dt1, makeReturns = TRUE)
Error in setkeyv(x, cols, verbose = verbose, physical = physical) : 
some columns are not in the data.table: DT

【问题讨论】:

标签: r data.table xts


【解决方案1】:

我们可以使用

library(xts)
library(data.table)
dt1 <- as.data.table(fortify.zoo(xt1))

-输出

str(dt1)
Classes ‘data.table’ and 'data.frame':  5 obs. of  4 variables:
 $ Index : POSIXct, format: "2019-03-04 16:00:00" "2019-03-04 16:01:00" "2019-03-04 16:02:00" "2019-03-04 16:03:00" ...
 $ EURAUD: num  1.6 1.6 1.6 1.6 1.6
 $ EURCAD: num  1.51 1.51 1.51 1.51 1.51
 $ EURCHF: num  1.13 1.13 1.13 1.13 1.13

【讨论】:

  • 以及如何更改索引的名称?
  • ?setnames函数
  • 但是,在其他函数中使用 data.table 时,setkeyv 参数出现错误。
猜你喜欢
  • 2016-11-23
  • 2017-10-30
  • 2018-01-17
  • 2013-12-10
  • 2019-07-21
  • 2017-02-12
  • 2016-01-07
  • 1970-01-01
  • 2012-03-06
相关资源
最近更新 更多