Skip to content
Prev 199852 / 398506 Next

Unexpected behaviour for as.date()

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

            
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
David Winsemius, MD
Heritage Laboratories
West Hartford, CT