0

I have data in the xts format as follows:

                     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

I need a small help in converting xts to data.table for not understanding the setkeyv argument.

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 I am obtaining the following error when I am using the data.table (after converting from xts to data.table) in other functions.

> 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
Shahal
  • 31
  • 5
  • `as.data.table` works directly with `xts`, tested on v1.14.0 : upgrade `data.table`? see https://www.rdocumentation.org/packages/data.table/versions/1.14.0/topics/as.data.table.xts – Waldi May 15 '21 at 22:59

1 Answers1

2

We can use

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

-output

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
akrun
  • 874,273
  • 37
  • 540
  • 662