请考虑以下示例:
structure(NA_real_, class = "Date")
## [1] NA
structure(Inf, class = "Date")
## [1] NA
is.na(structure(NA_real_, class = "Date"))
## [1] TRUE
is.na(structure(Inf, class = "Date"))
## [1] FALSE
Run Code Online (Sandbox Code Playgroud)
两者都打印为NA.这是预期的行为还是这个错误?看到NA一些不会返回的东西是非常烦人TRUE的is.na().
Jos*_*ich 10
这是预期的行为.什么是印刷不是对象是什么.要打印,对象需要转换为字符. as.character.Date电话format.Date,电话format.POSIXlt.(或)的价值部分说:?format.POSIXlt?strptime
表示时间的
format方法和strftime返回字符向量.NA时间返回为NA_character_.
这NA就是打印的原因,因为打印structure(NA_real_, class = "Date")返回NA_character_.例如:
R> is.na(format(structure(Inf, class = "Date")))
[1] TRUE
R> is.na(format(structure(NaN, class = "Date")))
[1] TRUE
Run Code Online (Sandbox Code Playgroud)
如果你以某种方式在你的代码中遇到这些糟糕的日期,我建议你使用is.finite而不是使用它来测试它们is.na.
R> is.finite(structure(Inf, class = "Date"))
[1] FALSE
R> is.finite(structure(NaN, class = "Date"))
[1] FALSE
Run Code Online (Sandbox Code Playgroud)