Werner Grundlingh
2018-Jun-08 21:21 UTC
[Rd] Date class shows Inf as NA; this confuses the use of is.na()
Indeed. as_date is from lubridate, but the same holds for as.Date. The output and it's interpretation should be consistent, otherwise it leads to confusion when programming. I understand that the difference exists after asking a question on Stack Overflow: https://stackoverflow.com/q/50766089/914686 This understanding is never mentioned in the documentation - that an Inf date is actually represented as NA: https://www.rdocumentation.org/packages/base/versions/3.5.0/topics/as.Date So I'm of the impression that the display should be fixed as a first option (thereby providing clarity/transparency in terms of back-end and output), or the documentation amended (to highlight this) as a second option. [[alternative HTML version deleted]]
Joris Meys
2018-Jun-09 11:41 UTC
[Rd] Date class shows Inf as NA; this confuses the use of is.na()
Hi Werner, on ?is.na it says:> The default method for anyNA handles atomic vectors without a class andNULL. 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() - xTime 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 On Fri, Jun 8, 2018 at 11:21 PM, Werner Grundlingh <wgrundlingh at gmail.com> wrote:> Indeed. as_date is from lubridate, but the same holds for as.Date. > > The output and it's interpretation should be consistent, otherwise it leads > to confusion when programming. I understand that the difference exists > after asking a question on Stack Overflow: > https://stackoverflow.com/q/50766089/914686 > This understanding is never mentioned in the documentation - that an Inf > date is actually represented as NA: > https://www.rdocumentation.org/packages/base/versions/3. > 5.0/topics/as.Date > So I'm of the impression that the display should be fixed as a first option > (thereby providing clarity/transparency in terms of back-end and output), > or the documentation amended (to highlight this) as a second option. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >-- Joris Meys Statistical consultant Department of Data Analysis and Mathematical Modelling Ghent University Coupure Links 653, B-9000 Gent (Belgium) <https://maps.google.com/?q=Coupure+links+653,%C2%A0B-9000+Gent,%C2%A0Belgium&entry=gmail&source=g> ----------- Biowiskundedagen 2017-2018 http://www.biowiskundedagen.ugent.be/ ------------------------------- Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php [[alternative HTML version deleted]]
Joris Meys
2018-Jun-09 11:45 UTC
[Rd] Date class shows Inf as NA; this confuses the use of is.na()
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 alogical 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. On Sat, Jun 9, 2018 at 1:41 PM, Joris Meys <jorismeys at 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 > > > > > On Fri, Jun 8, 2018 at 11:21 PM, Werner Grundlingh <wgrundlingh at gmail.com> > wrote: > >> Indeed. as_date is from lubridate, but the same holds for as.Date. >> >> The output and it's interpretation should be consistent, otherwise it >> leads >> to confusion when programming. I understand that the difference exists >> after asking a question on Stack Overflow: >> https://stackoverflow.com/q/50766089/914686 >> This understanding is never mentioned in the documentation - that an Inf >> date is actually represented as NA: >> https://www.rdocumentation.org/packages/base/versions/3.5.0/ >> topics/as.Date >> So I'm of the impression that the display should be fixed as a first >> option >> (thereby providing clarity/transparency in terms of back-end and output), >> or the documentation amended (to highlight this) as a second option. >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-devel at r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-devel >> > > > > -- > Joris Meys > Statistical consultant > > Department of Data Analysis and Mathematical Modelling > Ghent University > Coupure Links 653, B-9000 Gent (Belgium) > > <https://maps.google.com/?q=Coupure+links+653,%C2%A0B-9000+Gent,%C2%A0Belgium&entry=gmail&source=g> > > ----------- > Biowiskundedagen 2017-2018 > http://www.biowiskundedagen.ugent.be/ > > ------------------------------- > Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php >-- Joris Meys Statistical consultant Department of Data Analysis and Mathematical Modelling Ghent University Coupure Links 653, B-9000 Gent (Belgium) <https://maps.google.com/?q=Coupure+links+653,%C2%A0B-9000+Gent,%C2%A0Belgium&entry=gmail&source=g> ----------- Biowiskundedagen 2017-2018 http://www.biowiskundedagen.ugent.be/ ------------------------------- Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php [[alternative HTML version deleted]]