resolve_tendency: Resolves messy dates into a central tendency

resolve_tendencyR Documentation

Resolves messy dates into a central tendency

Description

These functions resolve messydates by their central tendency. While the functions mean(), median(), and modal() expand all elements of the vector into one combined set of dates and summarise it to a single value (matching the usual behaviour of these generics), the ⁠v*()⁠ versions resolve each element separately and so return a vector of the same length as the input.

Usage

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

vmedian(..., na.rm = TRUE)

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

## S3 method for class 'mdate'
mean(..., trim = 0, na.rm = TRUE, uncertain = "keep", approx_range = 0)

vmean(..., na.rm = TRUE)

## S3 method for class 'mdate'
vmean(..., trim = 0, na.rm = TRUE, uncertain = "keep", approx_range = 0)

modal(..., na.rm = TRUE)

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

vmodal(..., na.rm = TRUE)

## S3 method for class 'mdate'
vmodal(..., na.rm = TRUE, 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.

trim

the fraction (0 to 0.5) of observations to be trimmed from each end of x before the mean is computed. Values of trim outside that range are taken as the nearest endpoint.

Details

All of these functions work by calling compatible_dates() to enumerate the dates or date-times consistent with each messy value, then summarising that expanded set. For median() and mean(), an even number of expanded values is resolved by averaging the two middle values (via POSIXct when a time of day is present, or Date otherwise); an odd number simply returns the middle value.

Both kinds of set are expanded to their members and summarised the same way, but the result means different things for each. For a {} ("all members of") set, which records that something happened on several dates, the central tendency describes where those occurrences sit. For a ⁠[]⁠ ("one member of") set, which records that something happened on exactly one of those dates but not which, the central tendency is a point estimate of that single unknown date, as it is for a range or an unspecified component.

median() and vmedian() do not average two middle values across the BCE/CE boundary or between two BCE dates: they give the earlier of the two. mean() and vmean() average dates before the common era by day, and drop the time of day where a date before the common era is present.

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.

Approximate values

With approx_range above 0, an approximate value (~ or ⁠%⁠) widens by approx_range as in compatible_dates(), and its dates get the trapezoidal weights that vrandom() draws from: a weight of 1 across the dates that the value names, falling linearly to 0 over the widening. mean(), vmean(), median() and vmedian() then give the weighted mean and median. The dates with the highest weight are always those that the value names, so modal() and vmodal() give the same result with any approx_range.

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
# the time of day is honoured when averaging precise date-times
r <- as_messydate(c("2012-06-01 09:00", "2012-06-01 17:00"))
median(r)
mean(r)
median(d)
vmedian(d)
mean(d)
vmean(d)
modal(d)
vmodal(d)

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