Return NA for the min and max of a date vector if no nonmissing values are present.

date_min_with_nas(x)
date_max_with_nas(x)
date_range_with_nas(x)
min_with_nas_numeric(x)
max_with_nas_numeric(x)
range_with_nas_numeric(x)
min_with_nas_integer(x)
max_with_nas_integer(x)
range_with_nas_integer(x)

Arguments

x

The input date vector. Required

Value

A date value, that's possibly NA.

Note

This function is a workaround for a weakness in base::min.date() and base::max.date(). If no nonmissing values are present, both functions return +/-Inf, but print NA. These two function return and print NA, which behaves like SQL (and probably matches the expectations of most users).

See Stack Overflow Questions Using dplyr::group_by() to find min dates with NAs and R Inf when it has class Date is printing NA.

The foundation of these functions was proposed in a response by Edward Visel (SO username alistaire).

Author

Edward Visel, Will Beasley

Examples

library(OuhscMunge)
date_min_with_nas(c(NA, NA, NA))
#> [1] NA
date_min_with_nas(as.Date(NA_character_))
#> [1] NA
date_min_with_nas(as.Date(character(0)))
#> [1] NA
date_min_with_nas(as.Date(c("2009-04-21", "2017-12-27", NA_character_)))
#> [1] "2009-04-21"