[R] Dropping time series observations

Gabor Grothendieck ggrothendieck at gmail.com
Sun Feb 1 00:09:08 CET 2015


On Sat, Jan 31, 2015 at 2:16 PM, Mikael Olai Milhøj
<mikaelmilhoj at gmail.com> wrote:
> Hi
>
> Is there an easy way to drop, for instance, the first 4 observation of a
> time series object in R? I have tried to google the answer without any
> luck.
>
> To be more clear:
> Let us say that I have a time seres object x which data from 2000Q1 to
> 2014Q4 but I only want the data from 2001Q1 to 2014Q4.How do I remove the
> first four elements?
>
> By using x[5:?] R no longer recognize it as a ts.object.
>

1.  We could convert it to a zoo series, drop the first 4 and convert back:

For example, using the built in presidents ts series:

library(zoo)
as.ts(tail(as.zoo(presidents), -4))

1a. This would work too:

library(zoo)
as.ts(as.zoo(presidents)[-(1:4)])


2. Using only base R one can use window like this since 4 observations
is one cycle (given that the frequency of the presidents dataset is 4.

window(presidents, start = start(presidents) + 1)

or in terms of 4:

window(presidents, start = start(presidents) + 4 * deltat(presidents))

Here deltat is the time between observations so we want to start 4
deltat's later.




-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list