[Rd] Bug in as.Date or strptime?

Rui Barradas ruipb@rr@d@@ @ending from @@po@pt
Fri Jun 22 15:55:32 CEST 2018


Hello,

This just came up in SO, sessionInfo() at the end.

https://stackoverflow.com/questions/50988018/seeking-explanation-for-as-date-function-in-r?noredirect=1#comment88971055_50988018



# example 1
# not even the month is right
as.Date(x =  1, format = '%j', origin= '2015-01-01')
#[1] "2018-07-21"

# example 2a
# nonsense output
as.Date(x = 1, origin= '2015-01-01')
#[1] "2015-01-02"

# example 2a
# nonsense output, see example 6 below
as.Date(x = 1, origin = as.Date('2015-01-01'))
#[1] "2015-01-02"



# example 3
# I know that the method as.Date.numeric doesn't have
# argument 'format' but does have the dots argument.
# The format is passed on to strptime so maybe the problem is there.
as.Date(x =  1, format = '%j', origin= as.Date('2015-01-01'))
#[1] "2015-01-02"


# example 4
# Wrong, documented.
# origin should be automatically coerced to class 'Date'
# This is documented to behave like example 6 below
as.Date(x = '1',format = '%j', origin= '2015-01-01')
#[1] "2018-01-01"


# example 5
# right, documented. x of class 'character' needs argument 'format'
as.Date(x = '1', origin= '2015-01-01')
#Error in charToDate(x) :
#  string de caracteres não é um formato padrão não ambíguo


# example 6
# the safe way, the only one that outputs the right date
as.Date(x =  '1', format = '%j', origin= as.Date('2015-01-01'))
#[1] "2018-01-01"



sessionInfo()
R version 3.4.4 (2018-03-15)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.4 LTS

Matrix products: default
BLAS: /usr/lib/libblas/libblas.so.3.6.0
LAPACK: /usr/lib/lapack/liblapack.so.3.6.0

locale:
  [1] LC_CTYPE=pt_PT.UTF-8       LC_NUMERIC=C
  [3] LC_TIME=pt_PT.UTF-8        LC_COLLATE=pt_PT.UTF-8
  [5] LC_MONETARY=pt_PT.UTF-8    LC_MESSAGES=pt_PT.UTF-8
  [7] LC_PAPER=pt_PT.UTF-8       LC_NAME=C
  [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=pt_PT.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods
[7] base

loaded via a namespace (and not attached):
[1] compiler_3.4.4 tools_3.4.4    yaml_2.1.19


Or maybe I am missing something.

Thanks in advance,

Rui Barradas



More information about the R-devel mailing list