当 as.POSIXct() 工作时 lubridate::as_datetime() 失败?

lubridate::as_datetime() fails when as.POSIXct() works?

有人可以解释为什么 lubridate::as_datetime() 在这里失败了,但是 as.POSIXct() 有效吗?

> as.POSIXct("2020-10-27 20:25")
[1] "2020-10-27 20:25:00 CDT"

> lubridate::as_datetime("2020-10-27 20:25")
[1] NA
Warning message:
All formats failed to parse. No formats found. 

我不知道为什么一个有效而另一个无效,但您可以通过提供格式字符串来帮助 as_datetime() 理解输入,格式字符串指定文本字符串的格式。

lubridate::as_datetime("2020-10-27 20:25", format = "%Y-%m-%d %H:%M")

查看 as_datetime() 和 strptime() 的文档,了解如何编写 format-string。

编辑:as_datetime 的格式参数似乎默认为 NULL,如果提供格式 = NULL,as.Posixct() 会生成类似的错误。

as.POSIXct("2020-10-27 20:25", format = NULL)