[R] Unexpected behaviour for as.date()

David Winsemius dwinsemius at comcast.net
Wed Nov 11 05:58:04 CET 2009


On Nov 10, 2009, at 11:11 PM, Isabella Ghement wrote:

> Hi Berwin,
>
> Many thanks for your fast reply!  It's evening time in Vancouver,  
> Canada but
> it must be day time in Australia, so
> good day to you.
>
> Sorry about the confusion regarding the library name - I am using  
> the "date"
> library, as you pointed out.
>
> I tried the solution you suggested and find that am having problems  
> getting
> R to extract the year
> from an object created by as.date():
>
> d <- as.date(sub("-00", "-2000", "02-MAY-00"),order="dmy")
> strptime(d, "%d%b%y")$year

I do not have much, correction, make that no experience with the data  
package. I am wondering why you do not use the more "mainstream"  
function, as.Date:

 > d <- as.Date("02-MAY-07",format="%d-%b-%y")
 > d
[1] "2007-05-02"

It returns a Date formatted object that is the number of days from the  
origin, "1970-01-01". R generally "prefers" dates in the YYYY-MM-DD  
format, but allows conversion.

?Date

Differences and addition are supported:

 > d - 1:10
  [1] "2007-05-01" "2007-04-30" "2007-04-29" "2007-04-28" "2007-04-27"  
"2007-04-26" "2007-04-25"
  [8] "2007-04-24" "2007-04-23" "2007-04-22"
 > d + 1:10
  [1] "2007-05-03" "2007-05-04" "2007-05-05" "2007-05-06" "2007-05-07"  
"2007-05-08" "2007-05-09"
  [8] "2007-05-10" "2007-05-11" "2007-05-12"

 > Sys.Date() - d
Time difference of 923 days




>
>
> d <- as.date(sub("-07", "-2007", "02-MAY-07"),order="dmy")
> strptime(d, "%d%b%y")$year
>
> Try this as well, just in case:
>
> d <- as.date("02-MAY-07",order="dmy")
> strptime(d, "%d%b%y")$year
>
> How does one extract (a meaningful) year from the object d above?
>
> Kind regards,
>
> Isabella
>
> Isabella R. Ghement, Ph.D.
> Ghement Statistical Consulting Company
> -----Original Message-----
> From: Berwin A Turlach [mailto:berwin at maths.uwa.edu.au]
> Sent: November 10, 2009 7:13 PM
> To: Isabella Ghement
> Cc: r-help at r-project.org
> Subject: Re: [R] Unexpected behaviour for as.date()
>
>
> 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
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list