[R] Unexpected behaviour for as.date()

Berwin A Turlach berwin at maths.uwa.edu.au
Wed Nov 11 04:13:12 CET 2009


G'day Isabella,

On Tue, 10 Nov 2009 18:40:11 -0800
"Isabella Ghement" <isabella at ghement.ca> wrote:

> I am trying to use the function as.date() from the "dates" package 

As far as I can tell, there is no package called "dates", did you mean
the package "date"? 

> in R 2.10.0 to convert a character date to a Julian date, as follows:
> 
> > as.date("02-MAY-01", order="mdy") # convert May 2, 2001 to a Julian
> > date
> [1] 2May1

Are you sure that you are doing what your comments imply?  Try:

R> library(date)
R> as.date("02-MAY-01", order="mdy")
[1] 2May1
R> as.date("02-MAY-2001", order="mdy")
[1] 2May2001
R> as.numeric(as.date("02-MAY-01", order="mdy"))
[1] -21428
R> as.numeric(as.date("02-MAY-2001", order="mdy"))
[1] 15097
 
> However, when trying to convert a character date from the year 2000
> to a Julian date, I get an <NA> instead of the desired Julian date:
> 
> > as.date("02-MAY-00", order="mdy") # convert May 2, 2000 to a Julian
> > date
> [1] <NA>
> 
> Not quite sure why R is unable to handle this type of date (perhaps it
> thinks it comes from the year 1900?!).

My guess it thinks it comes from the year 0.  Not sure why it cannot
handle such dates.  But then, as far as I know, there is actually some
discussion about whether the year 0 exist or whether we went straight
from 1BC to 1AD......

> Is there a way to force R to convert character dates from the year
> 2000 into Julian dates? 

Presumably you will need something like:

R> as.date(sub("-00", "-2000", "02-MAY-00"))
[1] 2May2000

HTH.

Cheers,

	Berwin

========================== Full address ============================
Berwin A Turlach                      Tel.: +61 (8) 6488 3338 (secr)
School of Maths and Stats (M019)            +61 (8) 6488 3383 (self)
The University of Western Australia   FAX : +61 (8) 6488 1028
35 Stirling Highway                   
Crawley WA 6009                e-mail: berwin at maths.uwa.edu.au
Australia                        http://www.maths.uwa.edu.au/~berwin




More information about the R-help mailing list