parse_tag: Tag dates as approximate, uncertain, unspecified, or open

parse_tagR Documentation

Tag dates as approximate, uncertain, unspecified, or open

Description

Sources often give dates as if they were precise when they are not. For example, a dataset can have an arbitrary cut-off point for start and end dates. The ⁠tag_*()⁠ functions add the ISO 8601-2:2019(E) notation for this information to dates. Each ⁠tag_*()⁠ function has a matching ⁠date_is_*()⁠ test (see operate_statements).

  • tag_approximate() adds a tilde, ~, for a date or component that is possibly correct (e.g. 2003-03-~03 or ⁠2003-03-03~⁠). Test with date_is_approximate().

  • tag_uncertain() adds a question mark, ⁠?⁠, for a date or component that is possibly incorrect (e.g. 1916-10-?10 or ⁠1916-10-10?⁠). A date that is approximate and uncertain gets ⁠%⁠. Test with date_is_uncertain().

  • tag_unspecified() replaces the digits of a component with X (e.g. 2012-XX-15). This discards the digits. A trailing unspecified component is kept as XX, so tag_unspecified("2012-06-15", "day") gives 2012-06-XX. Test with !date_is_precise().

  • tag_before() makes a date into an open range that means "on or before" this date (e.g. ..1816-01-01). It does not mean "strictly before". Test with date_is_open().

  • tag_after() makes a date into an open range that means "on or after" this date (e.g. ⁠2016-12-31..⁠). Test with date_is_open().

  • untag() removes the tags that tags names. It cannot remove X, because tag_unspecified() discards the digits.

The empty set, {}, and NA do not change.

Usage

tag_approximate(x, component = NULL)

tag_uncertain(x, component = NULL)

tag_unspecified(x, component)

tag_before(x)

tag_after(x)

untag(x, tags = c("approximate", "uncertain", "before", "after"))

Arguments

x

A date vector.

component

The component to tag: "year", "month" or "day", or a group of date components: month and day ("md"), year and month ("ym"), or all of them ("date"). For date-times, component can also be "hour", "minute", or "second", or "time" for the whole time of day. By default, tag_approximate() and tag_uncertain() tag the finest component of each value, for example the day of 2012-06-15 (2012-06-~15) or the month of 2012-06 (2012-~06). Use component = "date" to tag the whole date (⁠2012-06-15~⁠). A component that the value does not have is not tagged, so tag_uncertain("2012", "day") gives 2012, and nor is an unspecified component, so tag_approximate("1993-01-XX") gives 1993-~01-XX. A tag that is already there is not added again, and ~ and ⁠?⁠ on the same component become ⁠%⁠. The result uses the fewest marks that tag the same components. Each end of a range and each member of a set is tagged. tag_unspecified() needs a component, and it does not accept "md", "ym", "date", or "time".

tags

The tags that untag() removes: any of "approximate", "uncertain", "before", and "after". The default removes all of them.

Value

An mdate object.

Examples

data <- data.frame(Beg = c("1816-01-01", "1916-01-01", "2016-01-01"),
  End = c("1816-12-31", "1916-12-31", "2016-12-31"))
transform(data, Beg = ifelse(Beg <= "1816-01-01",
  as.character(tag_before(Beg)), Beg))
transform(data, End = ifelse(End >= "2016-01-01",
  as.character(tag_after(End)), End))
transform(data, Beg = ifelse(Beg == "1916-01-01",
  as.character(tag_approximate(Beg)), Beg))
transform(data, End = ifelse(End == "1916-12-31",
  as.character(tag_uncertain(End)), End))
tag_approximate("2012-06-15")
tag_approximate("2012-06-15", "date")
tag_uncertain(tag_approximate("2012-06-15"))
tag_unspecified("2012-06-15", "day")
# time components can be tagged too
tag_approximate("2019-03-01 14:30:00", "hour")
tag_uncertain("2019-03-01 14:30:00", "second")
# remove tags
untag(c("2019-03-01%", "..2019-03-01"))
untag("2019-03-01%", "approximate")

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