【问题标题】:Selecting from xts by column name按列名从 xts 中选择
【发布时间】:2018-06-06 00:12:40
【问题描述】:

我试图在函数中按名称对 xts 对象中的特定列进行操作,但我不断收到错误消息:

Error in if (length(c(year, month, day, hour, min, sec)) == 6 && all(c(year, : missing value where TRUE/FALSE needed In addition: Warning messages: 1: In as_numeric(YYYY) : NAs introduced by coercion 2: In as_numeric(YYYY) : NAs introduced by coercion

如果我有一个 xts 对象:

xts1 <- xts(x=1:10, order.by=Sys.Date()-1:10)
xts2 <- xts(x=1:10, order.by=Sys.Date()+1:10)
xts3 <- merge(xts1, xts2)

然后我可以选择一个特定的列:

xts3$xts1

使用数据框,我可以将 xts3 传递给另一个函数,然后选择一个特定的列:

xts3['xts1']

但是如果我尝试对 xts 对象做同样的事情,我会得到上面的错误。例如

testfun <- function(xts_data){
  print(xts_data['xts1'])
}

调用:

testfun(xts3)

这行得通:

testfun <- function(xts_data){
  print(xts_data[,1])
}

但我真的很想按名称选择,因为我无法确定列顺序。

谁能建议如何解决这个问题?

谢谢!

【问题讨论】:

    标签: r xts


    【解决方案1】:

    xts-objects 具有类c("xts", "zoo"),这意味着它们是具有特殊属性的矩阵,这些属性由它们的创建函数分配。尽管$ 不会成功处理矩阵,但由于$.zoo 方法,它可以处理xtszoo 对象。 (也不建议在函数内部使用$,因为可能会造成名称评估混淆和部分名称匹配。)请参阅:?xts 并检查使用str 的第一个示例创建的sample.xts 对象:

    > ?xts
    starting httpd help server ... done
    > data(sample_matrix)
    > sample.xts <- as.xts(sample_matrix, descr='my new xts object')
    > 
    > str(sample.xts)
    An ‘xts’ object on 2007-01-02/2007-06-30 containing:
      Data: num [1:180, 1:4] 50 50.2 50.4 50.4 50.2 ...
     - attr(*, "dimnames")=List of 2
      ..$ : NULL
      ..$ : chr [1:4] "Open" "High" "Low" "Close"
      Indexed by objects of class: [POSIXct,POSIXt] TZ: 
      xts Attributes:  
    List of 1
     $ descr: chr "my new xts object"
    
     class(sample.xts)
    # [1] "xts" "zoo"
    

    这解释了为什么建议使用 xts3[ , "x"] 或等效的 xts3[ , 1] 的早期答案应该成功。 [.xts 函数首先提取“Data”元素,然后返回由j-参数指定的命名或编号列。

     str(xts3)
    An ‘xts’ object on 2018-05-24/2018-06-13 containing:
      Data: int [1:20, 1:2] 10 9 8 7 6 5 4 3 2 1 ...
     - attr(*, "dimnames")=List of 2
      ..$ : NULL
      ..$ : chr [1:2] "xts1" "xts2"
      Indexed by objects of class: [Date] TZ: UTC
      xts Attributes:  
     NULL
    > xts3[ , "xts1"]
               xts1
    2018-05-24   10
    2018-05-25    9
    2018-05-26    8
    2018-05-27    7
    2018-05-28    6
    2018-05-29    5
    2018-05-30    4
    2018-05-31    3
    2018-06-01    2
    2018-06-02    1
    2018-06-04   NA
    2018-06-05   NA
    2018-06-06   NA
    2018-06-07   NA
    2018-06-08   NA
    2018-06-09   NA
    2018-06-10   NA
    2018-06-11   NA
    2018-06-12   NA
    2018-06-13   NA
    

    merge.xts 操作可能未达到您的预期,因为日期范围没有重叠。您可能想要:

    > xts4 <- rbind(xts1, xts2)
    > str(xts4)
    An ‘xts’ object on 2018-05-24/2018-06-13 containing:
      Data: int [1:20, 1] 10 9 8 7 6 5 4 3 2 1 ...
      Indexed by objects of class: [Date] TZ: UTC
      xts Attributes:  
     NULL
    

    请注意,rbind.xts-操作未能交付具有共享列名的对象,因此需要数字访问。 (我希望有一个名为“Data”的元素,但你/我们还需要阅读 ?rbind.xts。)

    【讨论】:

    • 感谢您的补充说明。这很有意义,因为我已经看到索引也保存在对象中。我很欣赏不能在函数中使用 $ 表示法,但不明白为什么与 data.frame 不等价。
    • 不,他们不是。更正了那个错误。它们有 class= c("xts", "zoo") 所以它们实际上是具有特殊属性的矩阵,由它们的创建函数分配。
    【解决方案2】:

    键入?`[.xts`,您会看到该函数有一个i 和一个j 参数(等等)。

    i - 要提取的行。数字、基于时间或 ISO-8601 样式(查看详细信息)

    j - 要提取的列,数字或名称

    您将'xts1' 作为i 参数传递,而它应该是j。所以你的功能应该是

    testfun <- function(xts_data){
      print(xts_data[, 'xts1']) # or xts3[j = 'xts1']
    }
    

    【讨论】:

    • 这些陈述是正确的,但它们无法解释为什么提问者在使用基于 data.frame 的方法时会失败。 R [.data.frame 函数也接受 i 和 j 参数。
    • @42- 谢谢。我懂了。现在我需要深入挖掘。
    • 感谢您解决当前问题的答案,但如果可能的话,我想了解为什么行为也与 data.frame 不同。
    • @DrSimonHolgate 我想区别在于is.data.frame(xts3) 返回FALSEis.matrix(xts3) 返回TRUE,正如@42- 所述。然而,这让我感到困惑,因为我不明白为什么 xts3$xts1 会起作用。
    猜你喜欢
    • 2021-07-23
    • 1970-01-01
    • 2023-01-31
    • 2018-06-29
    • 1970-01-01
    • 2012-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多