Cbind () time series without NA

I noticed that for many operators on overlapping time series, the result is given only for the overlapping part, 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 is not like cbind() . As long as the output aligns correctly, NA are created for 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 execute this cbind() without creating strings with NA in them? Or, if not, what's a good way to take the result and delete the rows using NA s? This is not just a signature issue, because then it loses its temporary nature:

 > mts[complete.cases(mts),] ts1 ts2 [1,] 4 49 [2,] 5 36 

Maybe something with window() , but calculating the start and end times for a window seems a bit yucky. Any advice is appreciated.

+4
source share
1 answer

Why not just na.omit 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 can set this to FALSE and call stats:::.cbind.ts directly (after creating the 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 little easier .; -)

+4
source

All Articles