【问题标题】:Merge arbitrary number of xts objects of different lengths by index通过索引合并任意数量的不同长度的 xts 对象
【发布时间】:2021-05-31 05:16:31
【问题描述】:

我想将 3 个不同长度的 xts 对象合并为一个具有 3 列的 xts,并由第一个 xts 对象的日期索引。

a_xts <- xts(4:10, Sys.Date()+4:10)
b_xts <- xts(c(7:12), Sys.Date()+4:9)
c_xts <- xts(15:18, Sys.Date()+4:7)

我想保留 a_xts 索引日期。合并后的 xts 应如下所示:

           a_xts b_xts c_xts
2021-03-05     4     7    15
2021-03-06     5     8    16
2021-03-07     6     9    17
2021-03-08     7    10    18
2021-03-09     8    11    NA
2021-03-10     9    12    NA
2021-03-11    10    NA    NA

我的解决方案是逐一合并,为每个添加创建一个新的 xts 对象:

ab_xts <- merge(a_xts,b_xts, all = c(TRUE,FALSE))
abc_xts <- merge(ab_xts, c_xts, all = c(TRUE,FALSE))

有没有更好的方法来做到这一点,而无需转换为 .zoo 并返回 .xts 对象?

编辑

在合并所有 xts 对象时,我想只保留与 a_xts 的索引日期匹配的行,而不添加任何额外的索引日期。例如:

d_xts <- xts(c(7:18), Sys.Date()+4:15

d_xts 的长度大于 a_xts,而 b_xts 和 c_xts 的长度更短。通过逐个添加 xts 对象来继续我的解决方案,但似乎效率低下。

> abcd_xts <- merge(abc_xts, d_xts, all = c(TRUE,FALSE))
> abcd_xts
           a_xts b_xts c_xts d_xts
2021-03-05     4     7    15     7
2021-03-06     5     8    16     8
2021-03-07     6     9    17     9
2021-03-08     7    10    18    10
2021-03-09     8    11    NA    11
2021-03-10     9    12    NA    12
2021-03-11    10    NA    NA    13

【问题讨论】:

    标签: r merge xts


    【解决方案1】:

    我们可以使用以下内容(尽管对于问题中的特定示例,不需要 [...])。

    merge(a_xts, b_xts, c_xts)[time(a_xts), ]
    

    给予:

               a_xts b_xts c_xts
    2021-03-05     4     7    15
    2021-03-06     5     8    16
    2021-03-07     6     9    17
    2021-03-08     7    10    18
    2021-03-09     8    11    NA
    2021-03-10     9    12    NA
    2021-03-11    10    NA    NA
    

    【讨论】:

    • 正如我的编辑所解释的,我只需要包含a_xts 索引日期的行。此解决方案将在添加长度大于a_xts 的 xts 对象时添加索引日期。
    • 使用merge(a_xts, b_xts, c_xts)[time(a_xts), ](尽管在问题的示例中没有区别)。
    • merge(a_xts, b_xts, c_xts)[time(a_xts), ] 有效。你能发布答案,我会接受吗?
    【解决方案2】:

    我们可以将它放在list 中,并将mergeReduce 一起使用

    out <- Reduce(merge, mget(ls(pattern = '_xts$')))
    colnames(out) <- paste0(letters[1:3], "_xts")
    

    【讨论】:

      【解决方案3】:

      我发现merge() 可以在多个 xts 对象上使用参数all = c(TRUE,FALSE) 进行左外连接。这仅匹配传递给merge() 的第一个 xts 中的行。

      > merge(a_xts,b_xts,c_xts,d_xts, all = c(TRUE,FALSE))
      
                         a_xts b_xts c_xts d_xts
              2021-03-05     4     7    15     7
              2021-03-06     5     8    16     8
              2021-03-07     6     9    17     9
              2021-03-08     7    10    18    10
              2021-03-09     8    11    NA    11
              2021-03-10     9    12    NA    12
              2021-03-11    10    NA    NA    13
      

      【讨论】:

        猜你喜欢
        • 2017-12-08
        • 2020-11-06
        • 1970-01-01
        • 2012-08-15
        • 2011-09-16
        • 2014-03-09
        • 2016-11-09
        • 1970-01-01
        • 2017-03-18
        相关资源
        最近更新 更多