cbind() time series without NAs
r, time-series
Solution
Why not just `na.omit` the result?
> na.omit(cbind(ts1,ts2))
Time Series:
Start = c(2, 1)
End = c(2, 2)
Frequency = 3
ts1 ts2
2.000000 4 49
2.333333 5 36
If you want to avoid `na.omit`, `stats:::cbind.ts` calls `stats:::.cbind.ts`, which has a `union` argument. You could set that to `FALSE` and call `stats:::.cbind.ts` directly (after creating appropriate arguments):
> stats:::.cbind.ts(list(ts1,ts2),list('ts1','ts2'),union=FALSE)
Time Series:
Start = c(2, 1)
End = c(2, 2)
Frequency = 3
ts1 ts2
2.000000 4 49
2.333333 5 36
But the `na.omit` solution seems a tad easier. ;-)
Problem
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, `NA`s 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 `NA`s? 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.