简体   繁体   中英

how to input a list of time series to correlation function in r

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:)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM