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")
[1] "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?
我已经在包含这个错误消息的[R]标签中搜索并找到了28个其他问题。所有的解决方案和解决方案都涉及指定格式,iiuc。这个问题的不同之处在于,我问的是标准的明确格式定义在哪,它们能被改变吗?每个人都收到这些信息吗?还是只有我一个人?也许是与地区有关的?
In other words, is there a better solution than needing to specify the format?
换句话说,是否有比需要指定格式更好的解决方案?
29 questions containing "[R] standard unambiguous format"
29个包含“[R]标准明确格式”的问题
> 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
4 个解决方案
#1
52
This is documented behavior. From ?as.Date
:
这是记录行为。从? as.Date:
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.
格式:一个字符串。如果没有指定,它将尝试“%Y-%m-%d”“then”“%Y/%m/%d”“在第一个非‘NA’元素上,如果两个都不起作用,则给出一个错误。
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.
作为。日期(“2000年1月1日”)产生错误,因为格式不是上面列出的两种格式之一。日期(“01/01/2000”)给出一个错误的答案,因为日期不在上面列出的两种格式之一。
I take "standard unambiguous" to mean "ISO-8601" (even though as.Date
isn't that strict, as "%m/%d/%Y" isn't ISO-8601).
我认为“标准明确”是指“ISO-8601”(尽管如此)。日期并不严格,因为“%m/%d/%Y”不是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 ?LC_TIME
).
如果您收到了这个错误,那么解决方案就是指定您的日期(或datetimes)的格式,使用在strptime中描述的格式。如果您的数据包含日/月名称和/或缩写,那么一定要特别小心,因为转换将取决于您的语言环境(参见?strptime和read ?LC_TIME)。
#2
22
As a complement to @JoshuaUlrich answer, here is the definition of function as.Date.character
:
作为@JoshuaUlrich答案的补充,这里是函数的定义。
as.Date.character
function (x, format = "", ...)
{
charToDate <- function(x) {
xx <- x[1L]
if (is.na(xx)) {
j <- 1L
while (is.na(xx) && (j <- j + 1L) <= length(x)) xx <- x[j]
if (is.na(xx))
f <- "%Y-%m-%d"
}
if (is.na(xx) || !is.na(strptime(xx, f <- "%Y-%m-%d",
tz = "GMT")) || !is.na(strptime(xx, f <- "%Y/%m/%d",
tz = "GMT")))
return(strptime(x, f))
stop("character string is not in a standard unambiguous format")
}
res <- if (missing(format))
charToDate(x)
else strptime(x, format, tz = "GMT")
as.Date(res)
}
<bytecode: 0x265b0ec>
<environment: namespace:base>
So basically if both strptime(x, format="%Y-%m-%d")
and strptime(x, format="%Y/%m/%d")
throws an NA
it is considered ambiguous and if not unambiguous.
因此,基本上如果strptime(x, format="%Y-%m-%d")和strptime(x, format="%Y/%m/%d")抛出一个NA,它被认为是模糊的,如果不是明确的。
#3
16
In other words, is there a better solution than needing to specify the format?
换句话说,是否有比需要指定格式更好的解决方案?
Yes, there is now (ie in late 2016), thanks to anytime::anydate
from the anytime package.
是的,现在(在2016年下半年),感谢任何时间::任何日期从任何时候的包裹。
See the following for some examples from above:
下面是一些例子:
R> anydate(c("01 Jan 2000", "01/01/2000", "2015/10/10"))
[1] "2000-01-01" "2000-01-01" "2015-10-10"
R>
As you said, these are in fact unambiguous and should just work. And via anydate()
they do. Without a format.
正如你所说,这些实际上是明确的,应该是有效的。通过任何日期他们都可以。没有格式。
#4
2
Converting the date without specifying the current format can bring this error to you easily.
在不指定当前格式的情况下转换日期,可以很容易地将此错误传递给您。
Here is an example:
sdate <- "2015.10.10"
Convert without specifying the Format:
date <- as.Date(sdate4) # ==> This will generate the same error"""Error in charToDate(x): character string is not in a standard unambiguous format""".
Convert with specified Format:
date <- as.Date(sdate4, format = "%Y.%m.%d") # ==> Error Free Date Conversion.
#1
52
This is documented behavior. From ?as.Date
:
这是记录行为。从? as.Date:
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.
格式:一个字符串。如果没有指定,它将尝试“%Y-%m-%d”“then”“%Y/%m/%d”“在第一个非‘NA’元素上,如果两个都不起作用,则给出一个错误。
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.
作为。日期(“2000年1月1日”)产生错误,因为格式不是上面列出的两种格式之一。日期(“01/01/2000”)给出一个错误的答案,因为日期不在上面列出的两种格式之一。
I take "standard unambiguous" to mean "ISO-8601" (even though as.Date
isn't that strict, as "%m/%d/%Y" isn't ISO-8601).
我认为“标准明确”是指“ISO-8601”(尽管如此)。日期并不严格,因为“%m/%d/%Y”不是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 ?LC_TIME
).
如果您收到了这个错误,那么解决方案就是指定您的日期(或datetimes)的格式,使用在strptime中描述的格式。如果您的数据包含日/月名称和/或缩写,那么一定要特别小心,因为转换将取决于您的语言环境(参见?strptime和read ?LC_TIME)。
#2
22
As a complement to @JoshuaUlrich answer, here is the definition of function as.Date.character
:
作为@JoshuaUlrich答案的补充,这里是函数的定义。
as.Date.character
function (x, format = "", ...)
{
charToDate <- function(x) {
xx <- x[1L]
if (is.na(xx)) {
j <- 1L
while (is.na(xx) && (j <- j + 1L) <= length(x)) xx <- x[j]
if (is.na(xx))
f <- "%Y-%m-%d"
}
if (is.na(xx) || !is.na(strptime(xx, f <- "%Y-%m-%d",
tz = "GMT")) || !is.na(strptime(xx, f <- "%Y/%m/%d",
tz = "GMT")))
return(strptime(x, f))
stop("character string is not in a standard unambiguous format")
}
res <- if (missing(format))
charToDate(x)
else strptime(x, format, tz = "GMT")
as.Date(res)
}
<bytecode: 0x265b0ec>
<environment: namespace:base>
So basically if both strptime(x, format="%Y-%m-%d")
and strptime(x, format="%Y/%m/%d")
throws an NA
it is considered ambiguous and if not unambiguous.
因此,基本上如果strptime(x, format="%Y-%m-%d")和strptime(x, format="%Y/%m/%d")抛出一个NA,它被认为是模糊的,如果不是明确的。
#3
16
In other words, is there a better solution than needing to specify the format?
换句话说,是否有比需要指定格式更好的解决方案?
Yes, there is now (ie in late 2016), thanks to anytime::anydate
from the anytime package.
是的,现在(在2016年下半年),感谢任何时间::任何日期从任何时候的包裹。
See the following for some examples from above:
下面是一些例子:
R> anydate(c("01 Jan 2000", "01/01/2000", "2015/10/10"))
[1] "2000-01-01" "2000-01-01" "2015-10-10"
R>
As you said, these are in fact unambiguous and should just work. And via anydate()
they do. Without a format.
正如你所说,这些实际上是明确的,应该是有效的。通过任何日期他们都可以。没有格式。
#4
2
Converting the date without specifying the current format can bring this error to you easily.
在不指定当前格式的情况下转换日期,可以很容易地将此错误传递给您。
Here is an example:
sdate <- "2015.10.10"
Convert without specifying the Format:
date <- as.Date(sdate4) # ==> This will generate the same error"""Error in charToDate(x): character string is not in a standard unambiguous format""".
Convert with specified Format:
date <- as.Date(sdate4, format = "%Y.%m.%d") # ==> Error Free Date Conversion.