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

Roger Bivand Roger.Bivand at nhh.no
Mon Jun 7 18:19:42 CEST 2004


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




More information about the R-help mailing list