Reputation: 589
I have a list of time series:
ts1 <- ts(seq(1,12), start=c(2019,01), frequency=12)
ts2 <- ts(seq(12,26), start=c(2019,01), frequency=12)
ts3 <- ts(seq(24,30),start=c(2019,01), frequency=12)
list_ts <- list()
list_ts[["a"]] <- ts1
list_ts[["b"]] <- ts2
list_ts[["c"]] <- ts3
and I would like to obtain something with the same format as when specifying each element:
ts.intersect(ts1,ts2,ts3)
I tried this:
lapply(list_ts, ts.intersect)
but the output is not a matrix of time series as per my needs Thank you in advance :)
Upvotes: 2
Views: 54
Reputation: 886938
We can use do.call
do.call(ts.intersect, list_ts)
# a b c
#Jan 2019 1 12 24
#Feb 2019 2 13 25
#Mar 2019 3 14 26
#Apr 2019 4 15 27
#May 2019 5 16 28
#Jun 2019 6 17 29
#Jul 2019 7 18 30
Or with Reduce
Reduce(ts.intersect, list_ts)
Or using reduce
from purrr
library(purrr)
reduce(list_ts, ts.intersect)
Upvotes: 1