Skip to content

as.Date.character suggestion

8 messages · Duncan Murdoch, Peter Langfelder, Gabriel Becker +1 more

#
The default format="" in as.Date.character is supremely confusing. The
help shows as.Date defined as

## S3 method for class 'character'
     as.Date(x, format = "", ...)

yet the function behaves very differently when format is not specified
and when it is specified to its default:

as.Date("2012-01-01")
[1] "2012-01-01"
[1] "2014-08-20"  ### Gives today.


The default setting is never used, because if format is not given,
values "%Y-%m-%d" and "%Y/%m/%d" are tried for it (rather than "").

My suggestion is to leave out the default "" for format in as.Date:

as.Date(x, format, ...)

If this causes a conflict in the S3 dispatching, at least indicate in
the help that the default is never used and the function works
differently if format is specified to its default.

Thanks,

Peter
#
On 20/08/2014, 4:11 PM, Peter Langfelder wrote:
Wouldn't it be simpler to change the test from

if (missing(format))

to

if (format == "")

?

Duncan Murdoch
#
That would be my preferred solution, but it would break backwards
compatibility for format="", so others may disagree.

Peter

On Wed, Aug 20, 2014 at 1:18 PM, Duncan Murdoch
<murdoch.duncan at gmail.com> wrote:
#
Could do both to preserve bc...

if(missing(format) || !nchar(format))

~G


On Wed, Aug 20, 2014 at 1:50 PM, Peter Langfelder <
peter.langfelder at gmail.com> wrote:

            

  
    
#
On Wed, Aug 20, 2014 at 4:52 PM, Gabriel Becker <gmbecker at ucdavis.edu> wrote:
No, the problem is that the function behaves differently when format
is missing than when it equals its default. Removing this difference
necessarily changes behaviour and hence (at least in principle) breaks
backward compatibility.

Peter
#
Ah, my mistake, I read too fast. (My code is also wrong, embarassingly).

It seems like it's behavior when you pass it "" is simply a bug, then.

Sorry for the noise,

~G




On Wed, Aug 20, 2014 at 4:59 PM, Peter Langfelder <
peter.langfelder at gmail.com> wrote:

            

  
    
#
On 21 Aug 2014, at 02:21 , Gabriel Becker <gmbecker at ucdavis.edu> wrote:

            
It's not a bug, it's just that you are at the mercy of strptime() if you do specify a format. If the format doesn't contain a conversion for some component, you get the value corresponding to the current date and leftover characters are just ignored, e.g.
[1] "2014-01-01"

and the extreme case is that a "" format gives current date, whatever the input. On Mac OSX Mavericks anyway --- this stuff is system-dependent.

So I think Peter Langfelder is absolutely right, remove the default, which is never used anyway, and possibly update the documentation with a more direct reference to strptime(). This should have near-zero effect on the semantics. 

Peter D.

  
    
#
Now done, for R-devel only. This can't be high priority.

-pd
On 21 Aug 2014, at 09:28 , peter dalgaard <pdalgd at gmail.com> wrote: