[R] Vectors of years, months, and days to dates?

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Jun 7 17:44:09 CEST 2004

On Mon, 7 Jun 2004, Shin, Daehyok wrote:

> The interface for dates in R is a little confusing to me.
> I want to create a vector of Date objects from vectors of years, months, and
> days.
> One solution I found is:
> years <- c(1991, 1992)
> months <- c(1, 10)
> days <- c(1, 2)
> dates <- as.Date(ISOdate(years, months, days))
> But, in this solution the ISOdate function converts the vectors into
> characters,
> which can cause serious performance and memory loss
> when the vectors of years, months, and days are huge.

Really?  You have measured the loss?  A million causes no problem for 
example, and what are you going to do with a million dates that is 
instantaneous and worthwhile?  And a million dates are hardly going to be 
unique so you only need to convert the unique values.

> I am quite sure there is much better solution for it. What is it?

Write your own C code, or make a POSIXlt object directly from the numbers 
and convert that.

Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

More information about the R-help mailing list