Skip to content

Date conversion problem using "as.Date"

2 messages · Vegard Andersen, Gabor Grothendieck

#
Hello!

My problem is that the Julian date "behind" my dates seems to be wrong. I  
will examplify my problem.

t1 <- "1998-11-20"
t2 <- as.Date(t1)
# Here t2 is correctly "1998-11-20", but
date.mdy(t2)
$month
[1] 11
$day
[1] 19
$year
[1] 1988

And indeed, if I write: fix(t2) then I get : structure(10550, class =  
"Date"). So the Julian date is 10550, which is "1988-11-19", not the  
correct "1998-11-20"

If I instead of "as.Date" use "as.date", then things work ok. But I have  
not found out how to instruct "as.date" to handle dates from the 21st  
century.


I hope that someone can help me, thanks in advance!
#
Vegard Andersen <vegard.andersen <at> ism.uit.no> writes:

: 
: Hello!
: 
: My problem is that the Julian date "behind" my dates seems to be wrong. I  
: will examplify my problem.
: 
: t1 <- "1998-11-20"
: t2 <- as.Date(t1)
: # Here t2 is correctly "1998-11-20", but
: date.mdy(t2)
: $month
: [1] 11
: $day
: [1] 19
: $year
: [1] 1988
: 
: And indeed, if I write: fix(t2) then I get : structure(10550, class =  
: "Date"). So the Julian date is 10550, which is "1988-11-19", not the  
: correct "1998-11-20"
: 
: If I instead of "as.Date" use "as.date", then things work ok. But I have  
: not found out how to instruct "as.date" to handle dates from the 21st  
: century.
: 
: I hope that someone can help me, thanks in advance!
: 


As already mentioned the "date" class in survival uses 1960 as
its origin:

   R> as.date(0)
   [1] 1Jan60

whereas the "Date" class uses 1970:

   R> structure(0, class = "Date")
   [1] "1970-01-01"

Regarding your other question you can use a 4 digit year:

   R> as.date("2Jan2001")
   [1] 2Jan2001

or:

   R> as.date.Date <- function(x) as.date(format(x), order = "ymd")
   R> as.date.Date(t2)
   [1] 20Nov98