I’ve observed that for many operators on overlapping time series, the result is given only for the overlapping portion, which is nice:
> (ts1 <- ts(1:5, start=1, freq=3))
Time Series:
Start = c(1, 1)
End = c(2, 2)
Frequency = 3
[1] 1 2 3 4 5
> (ts2 <- ts((7:3)^2, start=2, freq=3))
Time Series:
Start = c(2, 1)
End = c(3, 2)
Frequency = 3
[1] 49 36 25 16 9
> ts1 + ts2
Time Series:
Start = c(2, 1)
End = c(2, 2)
Frequency = 3
[1] 53 41
However, this doesn’t seem to be the case with cbind(). While the output is aligned properly, NAs are created for the non-overlapping data:
> (mts <- cbind(ts1, ts2))
Time Series:
Start = c(1, 1)
End = c(3, 2)
Frequency = 3
ts1 ts2
1.000000 1 NA
1.333333 2 NA
1.666667 3 NA
2.000000 4 49
2.333333 5 36
2.666667 NA 25
3.000000 NA 16
3.333333 NA 9
Is there a way to perform that cbind() without creating the rows with NA in them? Or if not, what’s a good way to take the result and strip off the rows with the NAs? It’s not a simple matter of subscripting, because then it loses its timeseries nature:
> mts[complete.cases(mts),]
ts1 ts2
[1,] 4 49
[2,] 5 36
Maybe something with window(), but calculating the start & end times for the window seems a little yucky. Any advice is welcome.
Why not just
na.omitthe result?If you want to avoid
na.omit,stats:::cbind.tscallsstats:::.cbind.ts, which has aunionargument. You could set that toFALSEand callstats:::.cbind.tsdirectly (after creating appropriate arguments):But the
na.omitsolution seems a tad easier. 😉