gpt4 book ai didi

xts 对象列表中的 rbind.xts

转载 作者:行者123 更新时间:2023-12-04 15:32:04 28 4
gpt4 key购买 nike

我有一个 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

如何从列表中进行绑定(bind)?

最佳答案

我们可以使用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]])

关于xts 对象列表中的 rbind.xts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61069847/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com