Skip to content

Unexpected behaviour for as.date()

7 messages · Isabella Ghement, David Winsemius, Berwin A Turlach +1 more

#
Hi everyone,

I am trying to use the function as.date() from the "dates" package in R
2.10.0 to convert a character date to a Julian date, as follows:
[1] 2May1

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:
[1] <NA>

Not quite sure why R is unable to handle this type of date (perhaps it
thinks it comes from the year 1900?!).

Is there a way to force R to convert character dates from the year 2000 into
Julian dates?  I need to
perform this conversion in order to compute the difference between two
dates, one of which happens to come
from the year 2000.

Many thanks!

Isabella
#
G'day Isabella,

On Tue, 10 Nov 2009 18:40:11 -0800
"Isabella Ghement" <isabella at ghement.ca> wrote:

            
As far as I can tell, there is no package called "dates", did you mean
the package "date"?
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
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......
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
#
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


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
301-7031 Blundell Road, Richmond, B.C., Canada, V6Y 1J5
Tel: 604-767-1250
Fax: 604-270-3922
E-mail: isabella at ghement.ca
Web: www.ghement.ca










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

            
As far as I can tell, there is no package called "dates", did you mean
the package "date"?
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
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......
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
#
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
#
Hi David,

Thank you so much for your reply - the quick answer is that I inherited the
R code
I am working with from someone else so I am trying to use the same functions
they have used.  In hindsight, this did not make my life any easier - on the
contrary.
Of course, little did I know that I would find a bug in as.date() for the
year 2000
and that I would not be able to apply strptime() to objects produced by
as.date().
In the future, if given the option, I will stick with the as.Date() function
you suggested.

Kind regards,

Isabella


-----Original Message-----
From: David Winsemius [mailto:dwinsemius at comcast.net]
Sent: November 10, 2009 8:58 PM
To: Isabella Ghement
Cc: Berwin A Turlach; r-help at r-project.org
Subject: Re: [R] 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
http://www.R-project.org/posting-guide.html
David Winsemius, MD
Heritage Laboratories
West Hartford, CT
#
G'day Isabella,

On Tue, 10 Nov 2009 20:11:31 -0800
"Isabella Ghement" <isabella at ghement.ca> wrote:

            
Perhaps it would be best to first clarify what you really need. :)

If you have to extract the year, why go via as.date?  Why not just:

R> strsplit("02-MAY-00", "-")[[1]][3]
[1] "00"
R> as.numeric(strsplit("02-MAY-00", "-")[[1]][3])
[1] 0

Or if you have a vector of character strings, each a date in that
format:

R> x <- c("02-MAY-00", "02-MAY-01")
R> sapply(x, function(x) as.numeric(strsplit(x, "-")[[1]][3]))
02-MAY-00 02-MAY-01 
        0         1 

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