date_range: Find date ranges in the presence of missing subsets

date_rangeR Documentation

Find date ranges in the presence of missing subsets

Description

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

Usage

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(s)

Edward Visel, Will Beasley

Examples

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

OuhscBbmc/OuhscMunge documentation built on March 2, 2024, 11:44 a.m.