rbind.xts 来自 xts 对象列表
rbind.xts from list of xts objects
我有一个具有相同索引和列名的 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
如何从列表中绑定?
我们可以使用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 并对列进行平均:
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
如何从列表中绑定?
我们可以使用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]])