Re: [Rd] Date vs date

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Fri, 14 Sep 2007 19:39:13 -0400

On 9/14/07, Terry Therneau <therneau_at_mayo.edu> wrote:
> I wrote the date package long ago, and it has been useful. In my current task
> of reunifying the R (Tom Lumley) and Splus (me) code trees for survival, I'm
> removing the explicit dependence on 'date' objects from the expected survival
> routines so that they better integrate. Comparison of 'date' to 'Date' has
> raised a couple of questions.
>
> Clearly Date is more mature -- more options for conversion, better plotting,
> etc (a long list of etc). I see three things where date is better. Only the
> last of these really matters, and is the point on which I would like comment.
> (Well, actually I'd like to talk you all into a change, of course).
>
> 1. Since date uses 1/1/1960 as the base, and so does SAS, those of us who
> contantly pass files back and forth between those two packages have a slightly
> easier time.

There are some other programs that use 1/1/70. See the R Help Desk article in R News 4/1 that discusses a few origins.

>
> 2. as.date(10) works, as.Date(10) does not. Sometimes I have done a
> manipluation that the date package does not understand, and I know that the
> result is still of the right type, but the package does not. However, this is
> fairly rare and I can work around it. (It mostly occurs in processing the rate
> tables for expected survival).

You can define as.Date.numeric in your package and then it will work. zoo has done that.

library(zoo)
as.Date(10)

Some other things you can do:

today <- Sys.Date()
Epoch <- today - as.numeric(today)

Epoch + 10 # similar to as.Date(10)

>
> 3. temp <- as.Date('1990/1/1') - as.date('1953/2/5')
> sqrt(temp)
> Error in Math.difftime(temp3) : sqrtnot defined for "difftime" objects
>
> Minor bug: no space before the word 'not'
> Major: this shouldn't fail.
>
> People will do things with time intervals that you have not thought of. Fitting
> a growth curve that uses a square root, for instance. I firmly believe that the
> superior behavior in the face of something unexpected is to assume that the user
> knows what they are doing, and return a numeric.
> I recognize that "assume the user knows what they are doing" is an anathema
> to the more zealous OO types, but in designing a class I have found that they
> often know more than me!
>
> 4. Variation on #3 above
>
> > (as.Date('2007-9-14') - as.Date('1953-3-10')) / 365.25
> Time difference of 54.51335 days
>
> No, I am not 54.5 days old. Both hair color and knee creaking most
> definitely proclaim otherwise, I am sorry to say. Time difference / number
> should be a number.

Note that you can write:

x <- Sys.Date()
y <- x + 1
as.numeric(x-y)
as.numeric(x) - as.numeric(y)

>
> 5. This is only amusing. Im not saying that as.Date should necessarily work,
> but the format is certainly not ambiguous. (Not standard, but not ambiguous).
> Not important to fix, not something that date does any better.
>
> > as.Date('09Sep2007')
> Error in fromchar(x) : character string is not in a standard unambiguous format

as.Date("09Sep2007", "%d%b%Y")

>
>
>
> Terry Therneau



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Fri 14 Sep 2007 - 23:41:48 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 15 Sep 2007 - 08:41:00 GMT.

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