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 Thanks, Erin -- Erin Hodgess Associate Professor Department of Computer and Mathematical Sciences University of Houston - Downtown mailto: erinm.hodgess at gmail.com
Hi Erin, I am not sure what a "seq.Date object" is. My first thought is that you are talking about the date method for seq(), but there are hundreds of packages I do not know. In any case, here is what I think you want. Josh ## A small example is always nice dat <- ts(1:12, frequency = 12, start = c(1998, 1), end = c(2010, 12)) ## Achim and Gabor's wonderful package require(zoo) ## now just convert to a date as.Date(dat) On Wed, Mar 16, 2011 at 7: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 > > Thanks, > Erin > > > > -- > Erin Hodgess > Associate Professor > Department of Computer and Mathematical Sciences > University of Houston - Downtown > mailto: erinm.hodgess at gmail.com > > ______________________________________________ > R-help at 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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
On Mar 16, 2011, at 10:22 PM, Erin Hodgess 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/01Several (at least two, anyway) packages have functions to convert fractional years: RSiteSearch("fractional year dates") or you can use library(sos) Most date formats are in days so another option would be something along these lines conv.frac.yr <- function(yr) as.Date( (yr-1970)*(365 + (yr %% 4 == 0)), origin="1970-01-01" ) Unfortunately that doesn't take into account leap years in any except the current year so: > as.Date(sapply(c (1998.000, 2010.917), conv.frac.yr), origin="1970-01-01") [1] "1997-12-25" "2010-11-21" Not acceptable. Since others have already wrestled with this use their work to your advantage.> > Thanks, > Erin > > > > -- > Erin Hodgess > Associate Professor > Department of Computer and Mathematical Sciences > University of Houston - Downtown > mailto: erinm.hodgess at gmail.com > > ______________________________________________ > R-help at 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.David Winsemius, MD West Hartford, CT
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