R中字符串到日期转换的“标准明确日期”格式是什么?

问题描述 投票:79回答:5

请考虑以下事项

$ R --vanilla

> as.Date("01 Jan 2000")
Error in charToDate(x) :
    character string is not in a standard unambiguous format

但是那个日期显然是标准的明确格式。为什么出现错误信息?

更糟糕的是,一个模棱两可的日期显然是在没有警告或错误的情况下被接受的,然后读错了!

> as.Date("01/01/2000")
[1] "0001-01-20"

我搜索过并在包含此错误消息的[R]标签中发现了28个其他问题。所有涉及指定格式的解决方案和解决方法,iiuc。这个问题的不同之处在于,我在问无论如何定义标准的明确格式,它们可以改变吗?每个人都得到这些消息还是仅仅是我?也许它与语言环境有关?

换句话说,有没有比需要指定格式更好的解决方案?

29 questions containing "[R] standard unambiguous 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
r date date-formatting strptime as.date
5个回答
60
投票

这是记录在案的行为。来自?as.Date

format:字符串。如果未指定,它将在第一个非'''元素上尝试'“%Y-%m-%d”'然后''%Y /%m /%d“',如果两个元素都不起作用则给出错误。

as.Date("01 Jan 2000")产生错误,因为格式不是上面列出的两种格式之一。 as.Date("01/01/2000")产生错误答案,因为日期不是上面列出的两种格式之一。

我将“标准明确”称为“ISO-8601”(尽管as.Date不是那么严格,因为“%m /%d /%Y”不是ISO-8601)。

如果您收到此错误,解决方案是使用?strptime中描述的格式指定日期(或日期时间)的格式。如果您的数据包含日/月名称和/或缩写,请务必特别小心,因为转换将取决于您的区域设置(请参阅?strptime中的示例并阅读?LC_TIME)。


25
投票

作为@JoshuaUlrich答案的补充,这里是函数as.Date.character的定义:

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>

所以基本上如果strptime(x, format="%Y-%m-%d")strptime(x, format="%Y/%m/%d")都抛出一个NA它被认为是模棱两可的,如果不是毫不含糊的话。


25
投票

换句话说,有没有比需要指定格式更好的解决方案?

是的,现在(即2016年底),感谢来自anytime::anydate包装的anytime

有关上面的一些示例,请参阅以下内容:

R> anydate(c("01 Jan 2000", "01/01/2000", "2015/10/10"))
[1] "2000-01-01" "2000-01-01" "2015-10-10"
R> 

如你所说,这些实际上是明确无误的,应该可行。他们通过anydate()。没有格式。


4
投票

在不指定当前格式的情况下转换日期可以轻松地将此错误带给您。

这是一个例子:

sdate <- "2015.10.10"

转换时不指定格式:

date <- as.Date(sdate4) # ==> This will generate the same error"""Error in charToDate(x): character string is not in a standard unambiguous format""".

使用指定格式转换:

date <- as.Date(sdate4, format = "%Y.%m.%d") # ==> Error Free Date Conversion.

0
投票

这对我来说非常合适,无论以前如何编码日期。

library(lubridate)
data$created_date1 <- mdy_hm(data$created_at)
data$created_date1 <- as.Date(data$created_date1)
© www.soinside.com 2019 - 2024. All rights reserved.