【问题标题】:rbind.xts from list of xts objects来自 xts 对象列表的 rbind.xts
【发布时间】:2020-07-19 01:03:48
【问题描述】:

我有一个 xts 对象列表,这些对象具有共同的索引和列名。

我想按索引 rbind 并平均列:

dts = seq.POSIXt(from = Sys.time() - days(2), to = Sys.time(), by = 'day')
x1 = xts(x = 1:3, order.by = dts)
names(x1) = 'a'
x2 = xts(x = 2:4, order.by = dts)
names(x2) = 'a'

x = rbind.xts(x1, x2)  
aggregate(x, index(x), 'mean')

这很适合我想要的,问题是 xts 对象存储在列表中,而不是作为单独的对象。

如果我尝试

rbind.xts(list(x1, x2))

他们不加入:

[[1]]
                    a
2020-04-04 15:17:42 1
2020-04-05 15:17:42 2
2020-04-06 15:17:42 3

[[2]]
                    a
2020-04-04 15:17:42 2
2020-04-05 15:17:42 3
2020-04-06 15:17:42 4

如何从列表中进行 rbind?

【问题讨论】:

    标签: r xts zoo


    【解决方案1】:

    我们可以使用do.call

    do.call(rbind.xts, list(x1, x2))
    #                    a
    #2020-04-04 18:19:33 1
    #2020-04-04 18:19:33 2
    #2020-04-05 18:19:33 2
    #2020-04-05 18:19:33 3
    #2020-04-06 18:19:33 3
    #2020-04-06 18:19:33 4
    

    或者将list元素一一提取出来,应用rbind

    lst1 <- list(x1, x2)
    rbind.xts(lst1[[1]], lst1[[2]])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-11
      • 1970-01-01
      • 2014-09-01
      • 2019-09-02
      • 2016-11-30
      • 2011-11-25
      • 2018-12-06
      • 2020-11-15
      相关资源
      最近更新 更多