【问题标题】:R xts and data.tableR xts 和 data.table
【发布时间】:2012-03-06 21:39:34
【问题描述】:

我可以像处理 data.frame 一样将 data.table 转换为 xts 对象:

> 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"

在 xts 中使用 data.table 有什么问题吗?

【问题讨论】:

  • 没有问题,但它是 data.table 的事实丢失了:数据被转换为矩阵(在 xts 对象内)。在您的示例中,它甚至是一个字符串矩阵。
  • 我认为 xts 在其内部实现中保留了一个 data.frame 对象,并且只添加时间索引作为属性。我在 xts 本机而不是 data.frame 或 data.table 查询上运行的索引查询?
  • @RobertKubrick:xts 和它的父类 (zoo) 一样,使用带有索引属性的矩阵(不是 data.frame)。
  • 我明白了。所以我在 xts 列上运行的任何查询,例如 myxts[myxts$Var1 == "ABC" & myxts$Var2 == "123",] 实际上都是矩阵查询?
  • 如果您在 posixct 列上有带有键设置的 data.table,那不是涵盖了 xts 的功能吗?当然有很多为 xts 设计的功能,但从长远来看,在 posixct 上使用 dt 和 key 不是更好吗?

标签: r xts data.table


【解决方案1】:

只是为了解决一个悬而未决的问题。

正如文森特在评论中指出的那样,这没有问题。

它包含在 data.table 1.9.5 中。下面是类似的内容:

as.data.table.xts <- function(x, keep.rownames = TRUE){
  stopifnot(requireNamespace("xts") || !missing(x) || xts::is.xts(x))
  r = setDT(as.data.frame(x), keep.rownames = keep.rownames)
  if(!keep.rownames) return(r[])
  setnames(r,"rn","index")
  setkeyv(r,"index")[]
}

as.xts.data.table <- function(x){
  stopifnot(requireNamespace("xts") || !missing(x) || is.data.table(x) || any(class(x[[1]] %in% c("POSIXct","Date"))))
  colsNumeric = sapply(x, is.numeric)[-1] # exclude first col, xts index
  if(any(!colsNumeric)){
    warning(paste("Following columns are not numeric and will be omitted:",paste(names(colsNumeric)[!colsNumeric],collapse=", ")))
  }
  r = setDF(x[,.SD,.SDcols=names(colsNumeric)[colsNumeric]])
  rownames(r) <- x[[1]]
  xts::as.xts(r)
}

【讨论】:

  • 很好——+1。也许 Matt 和 Arun 可以将其拉入 data.table 本身?
  • as.data.table.xts 函数将索引转换为character,而as.xts.data.table 函数不允许xts 不是numeric 的对象(例如,所有character xts )
  • @DirkEddelbuettel 不确定,但返回此处的链接已添加到#882 以讨论...
  • 我可以有一个包含一列 xts 的 data.table 吗?
【解决方案2】:

由于 quantmod,通常会有一个 xts 并在所有列名中嵌入符号。 (例如“SPY.Open”、“SPY.High”等)。因此,这是 Jan 的 as.data.table.xts 的替代方案,它将符号放在单独的列中,这在 data.tables 中更自然(因为在进行任何分析之前,您可能会 rbind 一堆这些)。

as.data.table.xts <- function(x, ...) {
  cn <- colnames(x)
  sscn <- strsplit(cn, "\\.")  
  indexClass(x) <- c('POSIXct', 'POSIXt') #coerce index to POSIXct
  DT <- data.table(time=index(x), coredata(x))
  #DT <- data.table(IDateTime(index(x)), coredata(x))

  ## If there is a Symbol embedded in the colnames, strip it out and make it a 
  ## column
  if (all(sapply(sscn, "[", 1) == sscn[[1]][1])) {
    Symbol <- sscn[[1]][1]
    setnames(DT, names(DT)[-1], sub(paste0(Symbol, "."), "", cn))
    DT <- DT[, Symbol:=Symbol]
    setkey(DT, Symbol, time)[]
  } else {
    setkey(DT, time)[]
  }
}

library(quantmod)
getSymbols("SPY")
as.data.table(SPY)
            time   Open   High    Low  Close   Volume Adjusted Symbol
   1: 2007-01-03 142.25 142.86 140.57 141.37 94807600   120.36    SPY
   2: 2007-01-04 141.23 142.05 140.61 141.67 69620600   120.61    SPY
   3: 2007-01-05 141.33 141.40 140.38 140.54 76645300   119.65    SPY
   4: 2007-01-08 140.82 141.41 140.25 141.19 71655000   120.20    SPY
   5: 2007-01-09 141.31 141.60 140.40 141.07 75680100   120.10    SPY
  ---                                                                
1993: 2014-12-01 206.30 206.60 205.38 205.64 12670100   205.64    SPY
1994: 2014-12-02 205.81 207.34 205.78 207.09 72105500   207.09    SPY
1995: 2014-12-03 207.30 208.15 207.10 207.89 69450000   207.89    SPY
1996: 2014-12-04 207.54 208.27 206.70 207.66 89928200   207.66    SPY
1997: 2014-12-05 207.87 208.47 207.55 208.00 85031000   208.00    SPY

【讨论】:

    猜你喜欢
    • 2016-04-20
    • 2019-02-16
    • 2021-04-13
    • 2021-08-05
    • 2016-08-01
    • 1970-01-01
    • 2016-11-23
    • 2020-08-20
    • 1970-01-01
    相关资源
    最近更新 更多