[R] as.Date woes
    Peter Langfelder 
    peter.langfelder at gmail.com
       
    Wed Aug 20 20:56:09 CEST 2014
    
    
  
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:
> as.Date("2012-04-30")
[1] "2012-04-30"
> as.Date("2012-04-30", format = "")
[1] "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
arguments.
Here's what I get:
> as.Date( as.numeric(Sys.Date()), origin = "1970-1-1")
[1] "2014-08-20"   #### Correct
> as.Date( as.numeric(Sys.Date()), origin = "1970-1-1", format = "")
[1] "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??
> as.numeric(Sys.Date())
[1] 16302
> as.numeric(as.Date( as.numeric(Sys.Date()), origin = "1970-1-1"))
[1] 16302
> as.numeric(as.Date( as.numeric(Sys.Date()), origin = "1970-1-1", format = ""))
[1] 32604
Thanks in advance for any pointers!
Peter
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.
> sessionInfo()
R version 3.0.2 Patched (2013-10-08 r64039)
Platform: x86_64-unknown-linux-gnu (64-bit)
locale:
 [1] LC_CTYPE=en_US.utf8       LC_NUMERIC=C
 [3] LC_TIME=en_US.utf8        LC_COLLATE=en_US.utf8
 [5] LC_MONETARY=en_US.utf8    LC_MESSAGES=en_US.utf8
 [7] LC_PAPER=en_US.utf8       LC_NAME=C
 [9] LC_ADDRESS=C              LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.utf8 LC_IDENTIFICATION=C
attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base
    
    
More information about the R-help
mailing list