R `Inf` when it has class `Date` is printing `NA`
Asked Answered
r
S

1

17

Consider the following example:

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

Both are printing as NA. Is this the expected behavior or is this an error? It is very annoying to see NA for something that won't return TRUE for is.na().

Solidago answered 18/12, 2014 at 19:41 Comment(8)
So how exactly should print.Date display the "end of times".Conventioner
My best guess is because unclass(structure(Inf, class = "Date")) is not NA Inf is not really NA, but it is as far as the Date class goes...or something like thatArand
I don't see a method for is.na.Date. Not sure if that matters. Very good question!Arand
@BondedDust What's wrong with printing Inf?Chemistry
@Gregor, it is a philosophical question. Inf basically saying that time is infinite, while NA is basically saying "I don't know and I don't have a way to find out".Shot
Don't even need to unclass it: as.numeric(structure(Inf, class = "Date")) returns Inf .Stunk
But we do know some things, more than we know about typical NAs. Sys.Date() < structure(Inf, class = "Date") returns TRUE very appropriately.Chemistry
I think it is established that R knows that the structure is still Inf. Tracing the source of this behavior: print.date -> format.date -> as.POSIXltHynda
C
12

This is expected behavior. What is printed is not what the object is. To be printed, the object needs to be converted to character. as.character.Date calls format.Date, which calls format.POSIXlt. The Value section of ?format.POSIXlt (or ?strptime) says:

The format methods and strftime return character vectors representing the time. NA times are returned as NA_character_.

So that's why NA is printed, because printing structure(NA_real_, class = "Date") returns NA_character_. For example:

R> is.na(format(structure(Inf, class = "Date")))
[1] TRUE
R> is.na(format(structure(NaN, class = "Date")))
[1] TRUE

If you somehow encounter these wonky dates in your code, I recommend you test for them using is.finite instead of is.na.

R> is.finite(structure(Inf, class = "Date"))
[1] FALSE
R> is.finite(structure(NaN, class = "Date"))
[1] FALSE
Caviness answered 19/12, 2014 at 3:17 Comment(2)
Re "NA times are returned as NA_character_" in the doc, I don't think it's relevant here, since it's not a NA time as.Date(Inf)prints NAbut is.na(as.Date(Inf)) prints `FALSEBertrand
I think this goes back to how you define what a "NA time" means. I've seen lots of discussions that as.Date(Inf) is not a "NA time", but something different instead. I prefer a pragmatic approach, instead of arguing about what it is, or should be. That's why I recommended using is.finite() to determine whether or not a Date/POSIXt is valid.Caviness

© 2022 - 2024 — McMap. All rights reserved.