# Time series merge?

5 messages
Open this post in threaded view
|

## Time series merge?

 I have two time series   a <- ts(1:10, start=c(1,6), end=c(2,5), frequency=10) b <- ts(1:5, start=c(2,1), end=c(2,5), frequency=10)   Obviously 'b' is a subset of 'a'. I want a single index value indicating where that start of 'b' lines up with the start of 'a'. So in this simple example I would expect an index of 5. I was playing with 'merge'. But, for a 'ts' object this does not produce anything that is useful:   > merge(a,b)   x 1 1 2 2 3 3 4 4 5 5   I get the same answer if I use 'merge(b,a)' so I don't know how to convert this result to something useful. So then I decided to use 'xts'. But the conversion fails:   > ax <- as.xts(a) Error in as.xts.ts(a) : could not convert index to appropriate type   For this simple example I could code it myself using a simple for loop but if I add capability to handle missing dates, different frequencies, etc. it gets complicated very fast.  It seems that 'xts' has more extensive date handling facilities that 'ts' but I am stuck since it doesn't look like I can convert from 'ts' to 'xts'.   Thanks in advance for your suggestions.   Kevin         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Time series merge?

 On Sat, Nov 26, 2011 at 10:55 AM, Kevin Burton <[hidden email]> wrote: > I have two time series > > > > a <- ts(1:10, start=c(1,6), end=c(2,5), frequency=10) > > b <- ts(1:5, start=c(2,1), end=c(2,5), frequency=10) > > > > Obviously 'b' is a subset of 'a'. I want a single index value indicating > where that start of 'b' lines up with the start of 'a'. So in this simple > example I would expect an index of 5. I was playing with 'merge'. But, for a > 'ts' object this does not produce anything that is useful: > > > >> merge(a,b) > Try this: library(zoo) m <- merge(a = as.zoo(a), b = as.zoo(b)) m or to get a ts object back: as.ts(m) -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Time series merge?

 Seems to work fine. Thank you. -----Original Message----- From: Gabor Grothendieck [mailto:[hidden email]] Sent: Saturday, November 26, 2011 10:11 AM To: Kevin Burton Cc: [hidden email] Subject: Re: [R] Time series merge? On Sat, Nov 26, 2011 at 10:55 AM, Kevin Burton <[hidden email]> wrote: > I have two time series > > > > a <- ts(1:10, start=c(1,6), end=c(2,5), frequency=10) > > b <- ts(1:5, start=c(2,1), end=c(2,5), frequency=10) > > > > Obviously 'b' is a subset of 'a'. I want a single index value > indicating where that start of 'b' lines up with the start of 'a'. So > in this simple example I would expect an index of 5. I was playing > with 'merge'. But, for a 'ts' object this does not produce anything that is useful: > > > >> merge(a,b) > Try this: library(zoo) m <- merge(a = as.zoo(a), b = as.zoo(b)) m or to get a ts object back: as.ts(m) -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.