2016-07-20 23 views
1
library(data.table) 
library(xts) 

x <- xts(c(1:2,4:7,9:11), c(Sys.Date()+1:2,Sys.Date()+4:7,Sys.Date()+9:11)) 

as.data.table.xts(x) 
Error: could not find function "as.data.table.xts" 

Aber die Umwandlung von data.table zu xts Werke:Fehler bei der Umwandlung von xts data.table

y <- data.table(c(Sys.Date()+1:2,Sys.Date()+4:7,Sys.Date()+9:11),c(1:2,4:7,9:11)) 
as.xts.data.table(y) 
      V2 
2016-07-21 1 
2016-07-22 2 
2016-07-24 4 
2016-07-25 5 
2016-07-26 6 
2016-07-27 7 
2016-07-29 9 
2016-07-30 10 
2016-07-31 11 

packageVersion("data.table") 
[1] ‘1.9.7’ 

Antwort

7

Das Problem ist, dass Sie eine Methode direkt aufrufen, die Sie. as.data.table.xts wird nicht aus dem Namespace data.table exportiert, aber es ist als S3-Methode registriert. Das bedeutet, dass der Aufruf von as.data.table für ein xts-Objekt an as.data.table.xts gesendet wird.

identical(as.data.table(x), data.table:::as.data.table.xts(x)) 
# [1] TRUE 
+0

Danke für die Erklärung. – user227710

1

Wir as.data.table verwenden können. In data.table sind rownames nicht erlaubt, so dass es einen ‚Index‘ Spalte als die erste Spalte der Regel nicht tun sollte schafft

as.data.table(x) 
#  index V1 
#1: 2016-07-21 1 
#2: 2016-07-22 2 
#3: 2016-07-24 4 
#4: 2016-07-25 5 
#5: 2016-07-26 6 
#6: 2016-07-27 7 
#7: 2016-07-29 9 
#8: 2016-07-30 10 
#9: 2016-07-31 11 
+0

Danke. Dies bedeutet, dass as.data.table.xts (x) 'in' 1.9.7' entfernt und durch 'as.data.table (x)' ersetzt wurde. – user227710

+1

@ user227710 Es könnte der Fall sein, da ich auch die gleiche Version verwende. – akrun