r - Is it possible to merge two time series in one? -
i've been trying merge 2 ts objects, second 1 starts 1 period after next one. example, take following 2 time series
ts1<-ts(c(1:12),star=c(2014,1),freq=12) ts2<-ts(c(13:24),star=c(2015,1),freq=12)
as can see, both of them match in order make single ts out of 2 ts objects. thought logical answer rbind() function. makes matrix out of them, follows...
> rbind(ts1,ts2) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] ts1 1 2 3 4 5 6 7 8 9 10 11 12 ts2 13 14 15 16 17 18 19 20 21 22 23 24
i've tried without success other functions merge,cbind. using c() i've managed 1 timeseries, main problema lose structure attributes of original timeseries, bad because i'm trying use function forecast new ts gives me this:
error: variables ... specified different types fit
i happy being able add additional observations time series. adding value 13 ts1 january,2015 haven´t found how either.
i think funny because see natural thing ask ts object, haven´t found other question helps me out here. let´s hope not silly question.
you use xts package take care of details (for instance if series have gap)
library(xts) ts1<-as.xts(ts(c(1:12),star=c(2014,1),freq=12)) ts2<-as.xts(ts(c(13:24),star=c(2015,1),freq=12)) str(ts3 <- c(ts1, ts2)) # ‘xts’ object on jan 2014/dec 2015 containing: # data: int [1:24, 1] 1 2 3 4 5 6 7 8 9 10 ... # indexed objects of class: [yearmon] tz: # xts attributes: # null
cheers, peter
Comments
Post a Comment