[R] Unexpected behaviour for as.date()

Isabella Ghement isabella at ghement.ca
Wed Nov 11 05:11:31 CET 2009


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:

> 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




More information about the R-help mailing list