[R] as.Date woes
peter.langfelder at gmail.com
Wed Aug 20 21:27:57 CEST 2014
Never mind... the solution was to read the source code of
as.Date.character. It turns out the default format="" is meaningless.
If 'format' is not given in the call to as.Date, it is NOT assumed to
be "", and the function gives very different results from a call where
the argument format="" is given. Grrrr...
On Wed, Aug 20, 2014 at 11:56 AM, Peter Langfelder
<peter.langfelder at gmail.com> wrote:
> Hi all,
> I have recently started working with Date objects and find the
> experience unsettling, to put it mildly.
> The help for as.Date says, in part:
> ## S3 method for class 'character'
> as.Date(x, format = "", ...)
> x: An object to be converted.
> format: A character string. If not specified, it will try
> ‘"%Y-%m-%d"’ then ‘"%Y/%m/%d"’ on the first non-‘NA’ element,
> and give an error if neither works.
> If I read this correctly,
> as.Date("2012-04-30") and
> as.Date("2012-04-30", format = "")
> should give the same results, but they don't:
>  "2012-04-30"
>> as.Date("2012-04-30", format = "")
>  "2014-08-20"
> Note the latter gives today's date, without any warning or message.
> What method is called in the latter case?
> Another issue I am running into, that is probably connected to the
> 'format' argument above, is trying to convert a numeric or character
> in the same call. Basically, I would like to call
> as.Date(object, format = "", origin = "1970-1-1")
> where object can be a Date, numeric or character, in the hope that the
> appropriate method will be selected and will ignore unnecessary
> Here's what I get:
>> as.Date( as.numeric(Sys.Date()), origin = "1970-1-1")
>  "2014-08-20" #### Correct
>> as.Date( as.numeric(Sys.Date()), origin = "1970-1-1", format = "")
>  "2059-04-08" #### ???
> Excuse the coarse language, but WTF??? The first call confirms that
> the origin is specified correctly, and the second gives a date removed
> from the origin by twice the number of days than the actual input??
>  16302
>> as.numeric(as.Date( as.numeric(Sys.Date()), origin = "1970-1-1"))
>  16302
>> as.numeric(as.Date( as.numeric(Sys.Date()), origin = "1970-1-1", format = ""))
>  32604
> Thanks in advance for any pointers!
> PS: I know my R is not the most up to date, but I haven't found
> anything about Date mentioned in the changelog for the 3.x series.
> R version 3.0.2 Patched (2013-10-08 r64039)
> Platform: x86_64-unknown-linux-gnu (64-bit)
>  LC_CTYPE=en_US.utf8 LC_NUMERIC=C
>  LC_TIME=en_US.utf8 LC_COLLATE=en_US.utf8
>  LC_MONETARY=en_US.utf8 LC_MESSAGES=en_US.utf8
>  LC_PAPER=en_US.utf8 LC_NAME=C
>  LC_ADDRESS=C LC_TELEPHONE=C
>  LC_MEASUREMENT=en_US.utf8 LC_IDENTIFICATION=C
> attached base packages:
>  stats graphics grDevices utils datasets methods base
More information about the R-help