I have a list of xts objects that have index and column names in common.
I want to rbind by index and average the columns :
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')
This works well for what I want, the issue is that the xts objects are stored in a list and not as seperate objects.
If I try
rbind.xts(list(x1, x2))
They don't join:
[[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
How can I rbind from a list?
We can use 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
Or extract the list
elements one by one and apply rbind
lst1 <- list(x1, x2)
rbind.xts(lst1[[1]], lst1[[2]])