user35787
user35787

Reputation:

R time series intersect - list of ts objects

I have a list holding several ts objects, and want to get one single mts object that is the intersection (timewise) of them.

I have tried ts.intersect(MyList) but this does not work. Here is an example:

a1 = ts(rnorm(40), start=c(2001,1), freq=12)
a2 = ts(rnorm(15), start=c(2002,1), freq=12)
a3 = ts(rnorm(40), start=c(1999,1), freq=12)
List1 = list(a1,a2,a3)

# Does not work
b = ts.intersect(List1)

# This does work, but does not use the list object
b = ts.intersect(a1,a2,a3)

I have also tried ts.intersect(unlist(List1, recursive=FALSE)) which also does not work.

Upvotes: 0

Views: 2919

Answers (2)

G. Grothendieck
G. Grothendieck

Reputation: 270120

zoo has a multi-way merge so convert to zoo merge and convert back:

> library(zoo)
> List2 <- setNames(List1, c("a1", "a2", "a3"))
> as.ts(do.call("merge", Map(as.zoo, c(List2, all = FALSE))))
                 a1         a2         a3
Jan 2002 -1.2037687  1.5758712  1.2852462
Feb 2002 -0.5020486  0.2406350  0.4942195
Mar 2002 -1.3920381 -0.7291974 -0.1902032
Apr 2002 -0.9053585  0.2381535  1.6644473

Upvotes: 1

Sven Hohenstein
Sven Hohenstein

Reputation: 81733

You can use Reduce:

b <- Reduce(ts.intersect, List1)
# this is similar to ts.intersect(ts.intersect(List1[[1]],List1[[2]]), List1[3]])

The result:

          init.init init.x[[i]]     x[[i]]
Jan 2002 -0.3300873   0.1176911  1.7521511
Feb 2002 -0.1884980   1.3868065 -0.3621873
Mar 2002 -0.3318633   0.3086265 -0.2559493
Apr 2002 -1.1453912  -0.2793208 -1.0970463

You can also use do.call here:

b <- do.call(ts.intersect, List1)
# this is similar to ts.intersect(List1[[1]], List1[[2]], List1[3]])

But note that the names are quite long:

         structure(c(-1.80813067709511, -1.20282170077381, 0.138045873245257, 
Jan 2002                                                            -0.3300873
Feb 2002                                                            -0.1884980
Mar 2002                                                            -0.3318633
Apr 2002                                                            -1.1453912
         structure(c(0.117691115312119, 1.38680649563793, 0.308626529949489, 
Jan 2002                                                            0.1176911
Feb 2002                                                            1.3868065
Mar 2002                                                            0.3086265
Apr 2002                                                           -0.2793208
         structure(c(-0.639791317273505, 2.40288059297931, -1.15812150063018, 
Jan 2002                                                             1.7521511
Feb 2002                                                            -0.3621873
Mar 2002                                                            -0.2559493
Apr 2002                                                            -1.0970463

In both cases, you can change the names with:

colnames(b) <- c("a1", "a2", "a3")

                 a1         a2         a3
Jan 2002 -0.3300873  0.1176911  1.7521511
Feb 2002 -0.1884980  1.3868065 -0.3621873
Mar 2002 -0.3318633  0.3086265 -0.2559493
Apr 2002 -1.1453912 -0.2793208 -1.0970463

Upvotes: 1

Related Questions