Why do dates at infinity look like NAs but act like dates? [duplicate]

邮差的信 提交于 2019-11-30 01:51:31

问题


I was trying to figure out the best way to deal with Postgresql's ability to represent Infinity and -Infinity in their timestamps when using RPostgreSQL to bring data over into R. Along the way I found some strange behavior when trying to represent infinite dates in R.

I can attempt to create a date at negative and positive infinity in the following manner:

❥ as.Date(-1/0, origin="1970-01-01")
[1] NA
❥ as.Date(1/0, origin="1970-01-01")
[1] NA

They both appear to be NAs. However when comparing them, there seems to be an understanding that one is less than the other.

❥ as.Date(-1/0, origin="1970-01-01") < as.Date(1/0, origin="1970-01-01")
[1] TRUE
❥ as.Date(-1/0, origin="1970-01-01") > as.Date(1/0, origin="1970-01-01")
[1] FALSE
❥ as.Date(1/0, origin="1970-01-01") > as.Date("1970-01-01")
[1] TRUE
❥ as.Date(1/0, origin="1970-01-01") < as.Date("1970-01-01")
[1] FALSE

How does R know the difference, if they both convert to NA?


回答1:


They don't convert to NA, that's just how they're printed.

R> d <- as.Date(-Inf, origin="1970-01-01")
R> is.na(d)
# [1] FALSE
R> is.infinite(d)
# [1] TRUE

If you want them to print differently, you can override the print.Date method and add special cases for +/- infinity.



来源:https://stackoverflow.com/questions/30128851/why-do-dates-at-infinity-look-like-nas-but-act-like-dates

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!