为什么无限期的日期看起来像NAs,但是像日期一样行事? [英] Why do dates at infinity look like NAs but act like dates?
问题描述
我试图找出处理Postgresql的能力来代表 Infinity
和 -Infinity
的最佳方式他们使用 RPostgreSQL
将数据带入R时的时间戳。一路上,我发现一些奇怪的行为,当尝试在R中表示无限日期时。
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
他们都似乎是NAs。然而,当比较它们时,似乎有一个理解,一个是小于另一个。
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
如果他们都转换为NA,R是否知道差异?
How does R know the difference, if they both convert to NA?
推荐答案
他们不转换为 NA
,这就是它们的打印方式。
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
如果你想要他们打印不同的是,您可以覆盖 print.Date
方法,并为+/-无穷大添加特殊情况。
If you want them to print differently, you can override the print.Date
method and add special cases for +/- infinity.
这篇关于为什么无限期的日期看起来像NAs,但是像日期一样行事?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!