[Rd] Date class shows Inf as NA; this confuses the use of is.na()

Martin Maechler m@echler @ending from @t@t@m@th@ethz@ch
Mon Jun 11 11:12:23 CEST 2018


>>>>> Joris Meys 
>>>>>     on Sat, 9 Jun 2018 13:45:21 +0200 writes:

    > And now I've seen I copied the wrong part of ?is.na
    >> The default method for is.na applied to an atomic vector
    >> returns a
    > logical vector of the same length as its argument x,
    > containing TRUE for those elements marked NA or, for
    > numeric or complex vectors, NaN, and FALSE otherwise.

    > Key point being "atomic vector" here.

and a Date vector *is* atomic .. (so I'm confused about what
that issue is .. but read one.


    > On Sat, Jun 9, 2018 at 1:41 PM, Joris Meys
    > <jorismeys using gmail.com> wrote:

    >> Hi Werner,
    >> 
    >> on ?is.na it says:
    >> 
    >> > The default method for anyNA handles atomic vectors
    >> without a class and NULL.
    >> 
    >> I hear you, and it is confusing to say the least. Looking
    >> deeper, the culprit seems to be in the conversion of a
    >> Date to POSIXlt prior to the formatting:
    >> 
    >> > x <- as.Date(Inf,origin = '1970-01-01')
    >> > is.na(as.POSIXlt(x)) [1] TRUE
    >> 
    >> Given this implicit conversion, I'd argue that as.Date
    >> should really return NA as well when passed an infinite
    >> value. The other option is to provide an is.na method for
    >> the Date class, which is -given is.na is an internal
    >> generic- rather trivial:
    >> 
    >> > is.na.Date <- function(x) is.na(as.POSIXlt(x)) 
    >> > is.na(x) [1] TRUE
    >> 
    >> This might be a workaround for your current problem
    >> without needing changes to R itself. But this will give a
    >> "wrong" answer in the sense that this still works:
    >> 
    >> > Sys.Date() - x Time difference of -Inf days
    >> 

    >> I personally would go for NA as the "correct" date for an
    >> infinite value, but given that this will have
    >> implications in other areas, there is a possibility of
    >> breaking code and it should be investigated a bit further
    >> imho.  Cheers Joris

Indeed.  I could argue it is wrong to treat '+/- Inf' as NA for
dates (as well as for date times), because the Inf *does*
contain information in some sense:

     Infinitely far in the future
vs   Infinitely far in the past

which may make sense in some case ... in the same sense +Inf and
-Inf do make sense for numbers in some cases.

Martin



More information about the R-devel mailing list