什么是“标准明确日期"?R中字符串到日期转换的格式? [英] What are the "standard unambiguous date" formats for string-to-date conversion in R?
问题描述
请考虑以下事项
$ 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")
[1] "0001-01-20"
我在 [R] 标签中搜索并发现了其他 28 个包含此错误消息的问题.所有解决方案和解决方法都涉及指定格式,iiuc.这个问题的不同之处在于我要问的是,无论如何定义的标准明确格式在哪里,它们可以更改吗?每个人都收到这些消息还是只有我?也许它与语言环境有关?
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:
[1] LC_COLLATE=English_United Kingdom.1252
[2] LC_CTYPE=English_United Kingdom.1252
[3] LC_MONETARY=English_United Kingdom.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United Kingdom.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
推荐答案
这是记录在案的行为.从 ?as.Date
:
This is documented behavior. From ?as.Date
:
格式:一个字符串.如果未指定,它将尝试'%Y-%m-%d"'然后是%Y/%m/%d"在第一个非NA"元素上,如果两者都不起作用,则给出错误.
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")
产生错误,因为格式不是上面列出的两种格式之一.as.Date("01/01/2000")
产生不正确的答案,因为日期不是上面列出的两种格式之一.
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.
我接受标准明确"表示ISO-8601"(尽管 as.Date
没有那么严格,因为%m/%d/%Y"不是 ISO-8601).
I take "standard unambiguous" to mean "ISO-8601" (even though as.Date
isn't that strict, as "%m/%d/%Y" isn't ISO-8601).
如果您收到此错误,解决方案是使用?strptime
的详细信息 部分中描述的格式指定您的日期(或日期时间)的格式.
If you receive this error, the solution is to specify the format your date (or datetimes) are in, using the formats described in the Details section in ?strptime
.
确保转换规范的顺序以及任何分隔符与输入字符串的格式完全一致.此外,如果您的数据包含日/月名称和/或缩写,请务必特别小心,因为转换将取决于您的区域设置(请参阅 ?strptime
中的示例并阅读 ?LC_TIME
;另见 strptime
、as.POSIXct
和 as.Date
返回意外的 NA
).
Make sure that the order of the conversion specification as well as any separators correspond exactly with the format of your input string. Also, 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 ?LC_TIME
; see also strptime
, as.POSIXct
and as.Date
return unexpected NA
).
这篇关于什么是“标准明确日期"?R中字符串到日期转换的格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!