I have a datatable and now I would like to do some calculations in xts using the datatable syntax. My first question is if that is generally recommended, that means if the two packages work together nicely. An alternative would be to convert the datatable to xts and convert it backwards after the transformations I plan to do with xts.
The following simple DT illustrates my problem:
library(data.table)
dataset <- data.table(ID=c(rep("A",4416),rep("B",4416)),
x = c(rnorm(2208*2)), time=c(seq(as.Date("1988/03/15"),
as.Date("2000/04/16"), "day"),seq(as.Date("1988/03/15"),
as.Date("2000/04/16"), "day")))
dataset
library(xts)
dataset[,x_xts := NULL]
dataset[,x_xts := xts(x,order.by = time),by=ID]
dataset # this looks fine
str(dataset) # this throws an error
1) Can you recommend working with xts in datatable?
2) If not, would you recommend to transform the datatable to xts and back afterwards?
To provide more information, I want to estimate an ARIMA model for each unit in the panel, this is why I need to use xts I think. Thanks.
I think this str(dataset)
error it is more related to str
than the combination of data.table
and xts
If you use the alternative function glimpse
from dplyr
or make other operations, it works fine:
library(dplyr)
glimpse(dataset)
##Examples
cor(dataset$x,dataset$x_xts)
lm(formula = x~x_xts, data=dataset)
dataset[, A:=x_xts*x]
Anyway, you can also use the as.xts.data.table
function if you feel more comfortable that way: https://www.rdocumentation.org/packages/data.table/versions/1.12.0/topics/as.xts.data.table