Re: [R] as.Date() results depend on order of data within vector?

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Sun 07 Jan 2007 - 12:36:48 GMT

On Sun, 7 Jan 2007, Mark Wardle wrote:

> Dear all,
>
> The as.Date() function appears to give different results depending on
> the order of the vector passed into it.
>
> d1 = c("1900-01-01", "2007-01-01","","2001-05-03")
> d2 = c("", "1900-01-01", "2007-01-01","2001-05-03")
> as.Date(d1) # gives correct results
> as.Date(d2) # fails with error (* see below)
>
> This problem does not arise if the dates are NA rather than an empty
> string, but my data is coming via RODBC and I still don't have NAs
> passed across properly.
>
> I might add that I initially noticed this behaviour when using RODBC's
> sqlQuery() function call, and I initially had difficulty explaining why
> one column of dates was passed correctly, but another failed. The
> failing column was a "date of death" column where it was NA ("") for
> most patients.
>
> I've come up with two workarounds that work. The first is to sort the
> data at the SQL level, ensuring the initial record is not null. The
> second is to use sqlQuery() with as.is=T option, and then do the sorting
> and conversion afterwards.
>
> Is the behaviour of as.Date() shown above as expected/designed?

Yes. It uses the first non-NA string to choose the format *if you do not specify it*.

The correct work-around is to get non-valid strings returned as NA, not "". That is argument 'na.strings' in RODBC (and elsewhere: read.table behaves in the same way).

-- 
Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
R-help@stat.math.ethz.ch 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.
Received on Mon Jan 08 05:53:50 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Sun 07 Jan 2007 - 19:31:42 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.