Re: [Rd] [R] Date Format

From: Peter Dalgaard <p.dalgaard_at_biostat.ku.dk>
Date: Tue 11 Jul 2006 - 18:05:39 GMT

Martyn Plummer <plummer@iarc.fr> writes:

> I moved this to R-devel because I am wondering why the base package does
> not allow you to convert from numeric to Date. Could we not have
> something like this?
>
> as.Date.numeric <- function(x, epoch="1970-01-01", ...) {
> if (!is.character(epoch) || length(epoch) != 1)
> stop("invalid epoch")
> as.Date(epoch, ...) + x
> }

We could, but you might as well do it explicitly. There's something to be said for not confusing the concept of dates with a particular implementation, which is effectively what happens if you can convert them to and from numeric too seamlessly.

I'm more perplexed by the failure of adding difftimes to dates:

> as.Date("2006-1-1") + (as.Date("2006-1-1") - as.Date("2006-1-2"))
[1] "2005-12-31"
Warning message:

Incompatible methods ("+.Date", "Ops.difftime") for "+"

and if you have a difftime in non-days units, you'll actually get a wrong result:

> D1 <- as.Date("2006-1-1")
> D2 <- as.Date("2006-1-2")
> difftime(D2,D1,units="hours")

Time difference of 24 hours
> dd <- difftime(D2,D1,units="hours")
> D1+dd

[1] "2006-01-25"
Warning message:
Incompatible methods ("+.Date", "Ops.difftime") for "+"

> On Tue, 2006-07-11 at 12:58 -0400, Gabor Grothendieck wrote:
> > Try this:
> >
> > library(zoo)
> > as.Date(11328)
> >
> > See the Help Desk article in R News 4/1 for more on dates.
> >
> >
> > On 7/11/06, pierre clauss <pierreclauss@yahoo.fr> wrote:
> > > Hi everybody,
> > > I need your precious help for, I think, a simple request, but I do not manage to solve this.
> > >
> > > When I use a "table" function with dates in the rows, the rows are coerced to number after the table function.
> > >
> > > So I need to transform the row names into date format. But I do not manage.
> > >
> > > Therefore, for an example, I manage to write this :
> > >
> > > datetest<-"06/01/2001"
> > > datetest<-as.Date(datetest,"%d/%m/%Y")
> > > datetest<-as.numeric(datetest)
> > >
> > > to get 11328.
> > >
> > > But I do not obtain the inverse tranformation :
> > >
> > > datetest<-as.Date(datetest,"%d/%m/%Y")
> > >
> > > How do we get this please ?
> > >
> > > Thanks a lot for your solution.
> > > Pierre.
> > >
> > >
>
> -----------------------------------------------------------------------
> This message and its attachments are strictly confidential. ...{{dropped}}
>
> ______________________________________________
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

-- 
   O__  ---- Peter Dalgaard             ุster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk)                  FAX: (+45) 35327907

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Wed Jul 12 04:08:59 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Wed 12 Jul 2006 - 12:28:11 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.