From bolker at ufl.edu  Mon Jun 26 17:19:10 2006
From: bolker at ufl.edu (Ben Bolker)
Date: Mon, 26 Jun 2006 11:19:10 -0400
Subject: [Rd] Ops.Date: promote characters to Dates?
In-Reply-To: <Pine.LNX.4.64.0606261018380.21453@gannet.stats.ox.ac.uk>
References: <loom.20060624T165439-818@post.gmane.org>
	<971536df0606240823l14bfbd4mc1db38af7d23685f@mail.gmail.com>
	<449EF2F5.3010902@ufl.edu>
	<Pine.LNX.4.64.0606261018380.21453@gannet.stats.ox.ac.uk>
Message-ID: <449FFAEE.9090903@ufl.edu>

Prof Brian Ripley wrote:
> On Sun, 25 Jun 2006, Ben Bolker wrote:
> 

> Eh?  nchar is non-zero for almost all objects to be passed through here,
> including any coercible to Date.  (Remember nchar will itself coerce to
> character.)  And nchar(e1) will be a vector, not a scalar: perhaps
> is.character(e1) was what was intended?

  Yes.  I was following Ops.factor too closely and not thinking
carefully enough.

> I believe the issue is more general: naive users (those who confuse an
> object with its printed representation) expect to be able to compare
> classed objects to character strings, and for the classed objects to be
> converted by as.character() when doing so.

  (My suggestion actually went the other way, converting the character
string to a date: I think the results would be the same in this case
since the particular character representation yyyy-mm-dd sorts in
the same order as dates ...)

 That is not what is done:
> coerceVector(x, STRSXP) is used internally.  At some point we need to
> consider changing that, but it is not easy as method dispatch depends on
> the environment, something that is often not known when coerceVector is
> invoked.

 [snip as.numeric.Date]
>>   this takes a number of seconds since 1970 and
>> converts it into a Date object ...
> 
> That it would not do so is further evidence of the good reasons for not
> providing such a method for people to misinterpret.  (It would be the
> number of days since 1970-01-01, but probably one would want to take
> floor() of a number here.)

  oops again: typo.

   I think my main point is that there are several aspects of working
with dates at this point that are confusing to the naive user, and
it would be nice if there were a consistent way to make them
less confusing without breaking anything.  I only offered the
suggestions above to try to start a conversation about it.

For example, not only is

"1999-12-02"==as.Date("1999-12-03")

FALSE, but so is

"1999-12-02"<as.Date("1999-12-03")
(I believe it is equivalent to
"1999-12-02"<as.character(as.numeric(as.Date("1999-12-03")))

You could just say "don't do that"; I would submit that it is
a reasonable thing for a naive user to do (particularly when
they have been conditioned by having a similar strategy
work for factors) and could get them
into trouble.

  cheers
    Ben Bolker



[Rd] Ops.Date: promote characters to Dates?
Ben Bolker