[R] Unexpected behaviour for as.date()

Patrick Connolly p_connolly at slingshot.co.nz
Wed Nov 11 07:45:57 CET 2009


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.

> as.Date("02-May-00", "%d-%b-%y")
[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:

> as.Date("02-May-00", "%d-%b-%Y")
[1] "0-05-02"
> 

That goes some way to explaining a few things, though you might get
something else for the first one.

HTH

-- 
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.   
   ___    Patrick Connolly   
 {~._.~}                   Great minds discuss ideas    
 _( Y )_  	         Average minds discuss events 
(:_~*~_:)                  Small minds discuss people  
 (_)-(_)  	                      ..... Eleanor Roosevelt
	  
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.




More information about the R-help mailing list