Thanks Achim. Data manipulation in zoo and coerce back to ts. Sounds good!

On 1/10/08, Achim Zeileis <[EMAIL PROTECTED]> wrote:
>
> On Thu, 10 Jan 2008, tom soyer wrote:
>
> > Hi,
> >
> > I have two questions about ts.
> >
> > (1) How do I subset a ts object and still preserve the time index? for
> > example:
> >
> > > x=ts(1:10, frequency = 4, start = c(1959, 2)) # the ts object
> > > x
> >      Qtr1 Qtr2 Qtr3 Qtr4
> > 1959         1    2    3
> > 1960    4    5    6    7
> > 1961    8    9   10
> > I don't want the 1st 2 elements, so I could subset like this:
> > > x[3:length(x)]
> > [1]  3  4  5  6  7  8  9 10
> >
> > But then the time index is lost. I could use window(), but then I have
> to
> > specify start and end manually. Is there a way to subset a ts object so
> that
> > the time index is preserved for the data extracted without specifying
> start
> > and end data by hand?
>
> I think window() is the way to go with "ts".
> "zoo"/"zooreg" additionally provide what you ask for:
>
> library("zoo")
> x <- ts(1:10, frequency = 4, start = c(1959, 2))
> z <- as.zoo(x)
> z[3:length(z)]
>
> The time formatting is somewhat nice when you declare explicitely that
> this is "yearqtr" data:
>
> time(z) <- as.yearqtr(time(z))
> z[3:length(z)]
>
> > (2) How do I join two ts objects together end to end? for example:
> > > x=ts(1:10, frequency = 4, start = c(1959, 2)) # the 1st ts object
> > > y=ts(11:15, frequency = 4, start = c(1961, 4)) # the 2nd ts object
> >
> > As you can see, y is just a continuation of x. I would like to add y to
> the
> > end of x while preserving the time index. I could use this:
> > > ts(c(x,y),start=start(x),frequency=4)
> >
> > But I am wondering if there is a more efficient way of doing this, i.e.,
> is
> > it really necessary to specify start and frequency again when they are
> > already a part of the original ts objects?
>
> "zoo" also provides this:
>
> x <- ts(1:10, frequency = 4, start = c(1959, 2))
> y <- ts(11:15, frequency = 4, start = c(1961, 4))
> c(as.zoo(x), as.zoo(y))
>
> or you can also coerce back to "ts"
>
> as.ts(c(as.zoo(x), as.zoo(y)))
>
> Personally, I tend to do my data manipulations in "zoo" (not very
> surprisingly ;-)) but might coerce the resulting series to "ts" if I want
> to use certain modeling functions.
> Z
>
>
>
>


-- 
Tom

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to