当日期不正确时返回NA

Roc*_*nce 2 formatting datetime posix r

我正在尝试使用R命令as.POSIXct将字符串日期转换为POSIXct 如果列表中的某个日期不正确(2月31日),则返回错误.我怎样才能获得NA?

我在帮助中读到已知无效的日期时间将作为NA返回.这是一个错误吗?

as.POSIXct(c("2011-02-02", "2011-02-31"), tz="GMT")
Run Code Online (Sandbox Code Playgroud)

Tim*_*imo 8

您还应该为函数提供格式字符串,如下所示:

> as.POSIXct(c("2011-02-02", "2011-02-31"), tz="GMT", format='%Y-%m-%d')
[1] "2011-02-02 GMT" NA 
Run Code Online (Sandbox Code Playgroud)

问题是没有格式字符串,函数无法弄清楚,字符串的哪一部分代表月份,哪一部分代表一天.

编辑:

这实际上是错误抱怨的:

Error in as.POSIXlt.character(x, tz, ...) : 
  character string is not in a standard unambiguous format
Run Code Online (Sandbox Code Playgroud)