Posted By: Anonymous
Please consider the following
$ R --vanilla > as.Date("01 Jan 2000") Error in charToDate(x) : character string is not in a standard unambiguous format
But that date clearly is in a standard unambiguous format. Why the error message?
Worse, an ambiguous date is apparently accepted without warning or error and then read incorrectly!
> as.Date("01/01/2000")  "0001-01-20"
I’ve searched and found 28 other questions in the [R] tag containing this error message. All with solutions and workarounds involving specifying the format, iiuc. This question is different in that I’m asking where are the standard unambiguous formats defined anyway, and can they be changed? Does everyone get these messages or is it just me? Perhaps it is locale related?
In other words, is there a better solution than needing to specify the format?
> sessionInfo() R version 2.15.2 (2012-10-26) Platform: x86_64-w64-mingw32/x64 (64-bit) locale:  LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252  LC_MONETARY=English_United Kingdom.1252  LC_NUMERIC=C  LC_TIME=English_United Kingdom.1252 attached base packages:  stats graphics grDevices utils datasets methods base
This is documented behavior. From
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.
as.Date("01 Jan 2000") yields an error because the format isn’t one of the two listed above.
as.Date("01/01/2000") yields an incorrect answer because the date isn’t in one of the two formats listed above.
I take “standard unambiguous” to mean “ISO-8601” (even though
as.Date isn’t that strict, as “%m/%d/%Y” isn’t ISO-8601).
If you receive this error, the solution is to specify the format your date (or datetimes) are in, using the formats described in
?strptime. Be sure to use particular care if your data contain day/month names and/or abbreviations, as the conversion will depend on your locale (see the examples in
?strptime and read