[R] date conversion

Gabor Grothendieck ggrothendieck at gmail.com
Thu Mar 17 04:22:05 CET 2011


On Wed, Mar 16, 2011 at 10:22 PM, Erin Hodgess <erinm.hodgess at gmail.com> wrote:
> Dear R People:
>
> I have a monthly time series which runs from January 1998 to December 2010.
>
> When I use tsp I get the following:
>
>> tsp(ibm$ts)
> [1] 1998.000 2010.917   12.000
>
>
> Is there an easy way to convert this to a seq.Date object, please?
>
> I would like to have something to the effect of
> 1998/01/01 .... 2010/12/01
>

This would be clearer if you gave a complete example but creating a
sample series, ser, we can create a zoo series using Date class times
like this:

> library(zoo)
> ser <- ts(seq(12*13), start = 1998, frequency = 12)
> tsp(ser)
[1] 1998.000 2010.917   12.000
> z <- as.zoo(ser)
> time(z) <- as.Date(as.yearmon(time(ser)))
> head(z)
1998-01-01 1998-02-01 1998-03-01 1998-04-01 1998-05-01 1998-06-01
         1          2          3          4          5          6

You might actually prefer to leave it as "yearmon" class in which case its

> z <- as.zoo(ser)
> time(z) <- as.yearmon(time(z))
> head(z)
Jan 1998 Feb 1998 Mar 1998 Apr 1998 May 1998 Jun 1998
       1        2        3        4        5        6

-- 
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