resolve_extrema: Resolves messy dates into an extrema

resolve_extremaR Documentation

Resolves messy dates into an extrema

Description

This collection of S3 methods 'resolve' messy dates into a single date according to some explicit bias, such as returning the minimum or maximum date, the mean, median, or modal date, or a random date from among the possible resolutions for each messy date. If the date is not 'messy' (i.e. has no annotations) then just that precise date is returned. This can be useful for various descriptive or inferential projects.

Usage

vmin(..., na.rm = FALSE)

## S3 method for class 'mdate'
vmin(..., na.rm = FALSE, uncertain = "keep", approx_range = 0)

## S3 method for class 'mdate'
min(..., na.rm = FALSE, uncertain = "keep", approx_range = 0)

## S3 method for class 'mdate'
range(..., na.rm = FALSE, uncertain = "keep", approx_range = 0)

vmax(..., na.rm = FALSE)

## S3 method for class 'mdate'
vmax(..., na.rm = FALSE, uncertain = "keep", approx_range = 0)

## S3 method for class 'mdate'
max(..., na.rm = FALSE, uncertain = "keep", approx_range = 0)

Arguments

...

a mdate object

na.rm

Should NAs be removed? FALSE by default. As in base R, with na.rm = FALSE one NA makes the result of min() or max() NA.

uncertain

What to do with uncertain values (⁠?⁠ or ⁠%⁠), which the source itself doubts. "keep" (the default) resolves them as other values. "drop" gives NA for them in the element-wise ⁠v*()⁠ functions, and leaves them out of the functions that resolve the whole vector, such as min(). For random draws, see uncertain_p in vrandom().

approx_range

Range to widen approximate values (~ or ⁠%⁠) by, in units of the component that each mark covers, as in compatible_dates(). 0 (the default) ignores the marks. With a positive value, vmin() and vmax() give the first and last dates of the widened support, the same dates that compatible_dates() gives with the same approx_range, and that vrandom() can draw.

Details

vmin()/min() and vmax()/max() work directly on the annotated string (dropping ~/⁠?⁠/⁠%⁠ and, for vmax()/max(), filling in unspecified X components) rather than via compatible_dates(), which makes them considerably faster than resolving through the full expanded set. min() and max() further resolve a vector to a single, overall extremum (matching the usual behaviour of these generics), while vmin() and vmax() resolve each element separately.

Value

An mdate of precise dates or date-times. The ⁠v*()⁠ functions give one value for each element of x, with NA for a missing value and for the empty set, {}. The other functions give one value for the whole vector.

What min() and max() mean for messy dates

For a number, min() and max() give the smallest or largest value in a vector. For an mdate, they are resolution functions: min() gives the earliest date that any element of the vector can be, and max() gives the latest. The result is a precise date, and it need not be an element of the vector: min(as_messydate(c("2012", "2011-06"))) gives "2011-06-01", the first day of the earliest element, not the element "2011-06". range() gives min() and max() together.

This differs from ordering. sort() and order() order the elements themselves, and < gives NA when two elements overlap, because it cannot say which comes first. min() and max() do not compare the elements; they resolve the union of their possible dates. The package keeps the base names, and does not use prefixed names, because the meaning agrees with the base generics: each reduces a vector to one value of its class.

Dates that carry a time of day are already precise and so pass through these functions unchanged, keeping their time; a time of day plays no further role in choosing the minimum or maximum.

Examples

d <- as_messydate(c("2008-03-25", "?2012-02-27", "2001-01?", "2001~",
  "2001-01-01..2001-02-02", "{2001-01-01,2001-02-02}",
  "{2001-01,2001-02-02}", "2008-XX-31", "-0050-01-01"))
d
# a precise date-time is returned unchanged
vmin(as_messydate("2012-01-01 14:30:00"))
# an approximate date widens by approx_range
vmin(as_messydate("2001-05-16~"), approx_range = 3)
vmin(d)
min(d)
range(d)
vmax(d)
max(d)

messydates documentation built on Dec. 3, 2026, 5:09 p.m.