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

Roger Bivand Roger.Bivand at nhh.no
Mon Jun 7 19:02:22 CEST 2004


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

> > > res <- as.POSIXlt(paste(years, months, days, sep="-"))
> 
> This command still convert numbers to a character vector, right?

Yes, as Prof. Ripley said, the overhead of conversion to character and
using carefully crafted R functions is much less, for reasonable numbers
of dates, than inserting all the appropriate values into the POSIXlt class
object, since you need, in addition to years (since 1900), checked
month-days (31 February?), and months, week-days, year days, daylight
savings time flag, and seconds, minutes and hours. 

I would also be interested to know whether you can demonstrate
(system.time()) that anything reliable is faster than as.POSIXlt() for
fewer than millions of dates (and would you trust it on 29 February even
if it was faster?). Using base classes and functions is in general much
more robust than rolling your own, simply because many more people use
them - they have much more data run through them, and the time you might
save trying to avoid integer to character conversion will/should be eaten
up by debugging.

> 
> Daehyok Shin (Peter)
> Terrestrial Hydrological Ecosystem Modellers
> Geography Department
> University of North Carolina-Chapel Hill
> sdhyok at email.unc.edu
> 
> "We can do no great things, 
> only small things with great love."
>                          - Mother Teresa
> 
> > -----Original Message-----
> > From: Roger Bivand [mailto:Roger.Bivand at nhh.no]
> > Sent: Monday, June 07, 2004 PM 12:20
> > To: Daehyok Shin
> > Cc: R, Help
> > Subject: Re: [R] Vectors of years, months, and days to dates?
> > 
> > 
> > On Mon, 7 Jun 2004, Daehyok Shin wrote:
> > 
> > > How can I create POSIXlt directly from the numbers?
> > > I failed to find the solution from help documents.
> > 
> > ?POSIXlt
> > 
> > ?as.POSIXlt:
> > 
> > > res <- as.POSIXlt(paste(years, months, days, sep="-"))
> > > str(res)
> > `POSIXlt', format: chr [1:2] "1991-01-01" "1992-10-02"
> > > res$year 
> > [1] 91 92
> > 
> > > 
> > > Daehyok
> > > 
> > > --On Monday, June 07, 2004 4:44 PM +0100 Prof Brian Ripley 
> > > <ripley at stats.ox.ac.uk> wrote:
> > > 
> > > > 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
> > > >
> > > 
> > > ______________________________________________
> > > R-help at stat.math.ethz.ch mailing list
> > > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> > > PLEASE do read the posting guide! 
> > http://www.R-project.org/posting-guide.html
> > > 
> > 
> > -- 
> > Roger Bivand
> > Economic Geography Section, Department of Economics, Norwegian School of
> > Economics and Business Administration, Breiviksveien 40, N-5045 Bergen,
> > Norway. voice: +47 55 95 93 55; fax +47 55 95 93 93
> > e-mail: Roger.Bivand at nhh.no
> > 
> > 
> > 
> 

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Breiviksveien 40, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 93 93
e-mail: Roger.Bivand at nhh.no




More information about the R-help mailing list