Skip to content
Prev 199865 / 398506 Next

Unexpected behaviour for as.date()

G'day fellow Pacific rim dwellers,
On Wed, 11-Nov-2009 at 11:13AM +0800, Berwin A Turlach wrote:
|> G'day Isabella,
|> 

[...]

|> > 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......

I've not used the date package, but to do what's required, the base
function as.Date is adequate.
[1] "2000-05-02"
The help for strptime is informative:


     '%y' Year without century (00-99). If you use this on input, which
          century you get is system-specific.  So don't!  Often values
          up to 68 (or 69) are prefixed by 20 and 69 (or 70) to 99 by
          19.

     '%Y' Year with century.

On my system, I get this:
[1] "0-05-02"
That goes some way to explaining a few things, though you might get
something else for the first one.

HTH