[Rd] as.Date(Inf) displays as 'NA' but is actually 'Inf'

Richard White w @end|ng |rom rwh|te@no
Tue Mar 5 08:31:18 CET 2019


Hi,

I think I've discovered a bug in base R.

Basically, when using 'Inf' as as 'Date', is is visually displayed as 
'NA', but R still treats it as 'Inf'. So it is very confusing to work 
with, and can easily lead to errors:

# Visually displays as NA
 > as.Date(Inf, origin="2018-01-01")
[1] NA

# Visually displays as NA
 > str(as.Date(Inf, origin="2018-01-01"))
Date[1:1], format: NA

# Is NOT NA
 > is.na(as.Date(Inf, origin="2018-01-01"))
[1] FALSE

# Is still Inf
 > is.infinite(as.Date(Inf, origin="2018-01-01"))
[1] TRUE

This gets really problematic when you are collapsing dates over groups 
and you want to find the first date of a group. Because min() returns 
Inf if there is no data:

# Visually displays as NA
 > as.Date(min(), origin="2018-01-01")
[1] NA
Warning message: In min() : no non-missing arguments to min; returning Inf

# Visually displays as NA
 > str(as.Date(min(), origin="2018-01-01"))
Date[1:1], format: NA
Warning message: In min() : no non-missing arguments to min; returning Inf

# Is not NA
 > is.na(as.Date(min(), origin="2018-01-01"))
[1] FALSE
Warning message: In min() : no non-missing arguments to min; returning Inf

# This is bad!
 > as.Date(min(), origin="2018-01-01") > "2018-01-01"
[1] TRUE
Warning message: In min() : no non-missing arguments to min; returning Inf

Here is my sessionInfo():

 > sessionInfo()
R version 3.5.0 (2018-04-23)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Debian GNU/Linux 9 (stretch)
Matrix products: default
BLAS: /usr/lib/openblas-base/libblas.so.3
LAPACK: /usr/lib/libopenblasp-r0.2.19.so

locale:
[1] LC_CTYPE=C.UTF-8 LC_NUMERIC=C LC_TIME=C.UTF-8 LC_COLLATE=C.UTF-8 
LC_MONETARY=C.UTF-8
[6] LC_MESSAGES=C LC_PAPER=C.UTF-8 LC_NAME=C LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats graphics grDevices utils datasets methods base loaded via a 
namespace (and not attached):
[1] compiler_3.5.0 tools_3.5.0 yaml_2.1.19

 > Sys.getlocale()
[1] 
"LC_CTYPE=C.UTF-8;LC_NUMERIC=C;LC_TIME=C.UTF-8;LC_COLLATE=C.UTF-8;LC_MONETARY=C.UTF-8;LC_MESSAGES=C;LC_PAPER=C.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=C.UTF-8;LC_IDENTIFICATION=C"



More information about the R-devel mailing list