| coerce_to | R Documentation |
mdateThese methods coerce various date classes into the mdate class.
They represent the main user-facing class-creating functions in the package.
In addition to the typical date classes in R (Date, POSIXct, and POSIXlt),
there is also a direct method for converting text or character strings to mdate.
The function can also extract dates and times from text,
including some historical prose conventions,
in English or French (see lang).
as_messydate(x, resequence = FALSE, ...)
## S3 method for class 'Date'
as_messydate(x, resequence = FALSE, ...)
## S3 method for class 'POSIXct'
as_messydate(x, resequence = FALSE, ...)
## S3 method for class 'POSIXlt'
as_messydate(x, resequence = FALSE, ...)
## S3 method for class 'character'
as_messydate(x, resequence = NULL, pivot = NULL, lang = "en", ...)
## S3 method for class 'factor'
as_messydate(x, resequence = NULL, ...)
## S3 method for class 'numeric'
as_messydate(x, resequence = NULL, origin = NULL, ...)
## S3 method for class 'list'
as_messydate(x, resequence = FALSE, collapse = TRUE, ...)
mdate(x, resequence = FALSE, ...)
x |
A scalar or vector of a class that can be coerced into |
resequence |
Users have the option to choose the order for
ambiguous dates with or without separators (e.g. "11-01-12" or "20112112").
|
... |
Arguments passed to methods. |
pivot |
For character input, how to read two-digit years, such as
the "29" in "1/1/29" or the "97" in "May-97".
By default ( |
lang |
For character input, the language of any prose in |
origin |
For numeric input, the origin of a serial day count.
Spreadsheets store dates as a count of days from an origin,
and a file that loses its date formatting carries only these counts.
Use |
collapse |
For a list, should a range that covers a whole month or
year become that month or year? |
Coercion from POSIXct and POSIXlt preserves the time of day
(and UTC offset) as an ISO 8601-2 date-time.
Times of exactly midnight (00:00:00) are treated as date-only,
so that timezone-naive dates round-trip unchanged.
A mdate class object
as_messydate(): Core mdate class coercion function
as_messydate(Date): Coerce from Date to mdate class
as_messydate(POSIXct): Coerce from POSIXct to mdate class
as_messydate(POSIXlt): Coerce from POSIXlt to mdate class
as_messydate(character): Coerce character date objects to mdate class
as_messydate(factor): Coerce factors to mdate class, via their labels.
This is the common case when a date column has been read in with
stringsAsFactors = TRUE.
as_messydate(numeric): Coerce numeric objects to mdate class
as_messydate(list): Coerce a list of dates to the shortest mdate.
Each element of the list gives one element of the result.
This is the inverse of compatible_dates().
An empty element gives the empty set, "{}",
and an element that is only NA gives NA.
An unspecified month or day is kept as XX: "1993-01-XX" says that
the record has a day that is not known, while "1993-01" says only
that it names the month. A month or day written as 0, 00, ?? or
NA is read as XX. Both forms have the same possible dates, and the
resolution functions give the same results for them.
A set with one member keeps its braces: "{1993-01-01..1993-01-31}"
is all the days of January (a spell, such as a monthly count), and
"[1993-01-01..1993-01-31]" is one of them. A bare range, and a range
in prose ("from the 13th to the 15th of February"), is one of its days.
Functions that write a result from its days, such as +,
%intersect%, and as_messydate() on a list, write the shortest form,
without XX or braces.
Beyond plain and lightly-formatted dates, as_messydate() recognises
several conventions common in e.g. historical texts and converts them to
their ISO 8601-2 equivalent before the usual parsing takes place:
Roman numerals for a bare year, e.g. "MDCCLXXVI" becomes 1776.
Roman calendar references, i.e. the Kalends, Nones, and Ides of a
named month, e.g. "the Ides of March, 44 BC" becomes "-0043-03-15".
The Nones and Ides fall later (the 7th and 15th) in March, May, July,
and October, and earlier (the 5th and 13th) in other months.
Approximate qualifiers ("around", "circa", "about", "roughly", ...)
add the ~ annotation, and uncertain qualifiers ("possibly", "perhaps",
"reportedly", ...) add ?; both together add %, e.g.
"possibly about 1910" becomes "%1910".
Connectives joining two days of the same month: "between the 13th
and 15th" or "from the 13th to the 15th" become a range (..); "the
13th or the 15th" becomes a set ({}); and a plain "the 13th and the
15th" becomes two separate dates.
"before"/"prior to"/"no later than" and "after"/"since"/"no
earlier than" become an open range, e.g. "before 1910" becomes
"..1910". The bound may itself be any precision the parser
understands, including a decade or century.
Decades ("the 1920s" becomes "192X") and centuries ("the 19th
century" becomes "18XX").
A day of the year, e.g. "the 103rd day of 2026" becomes
"2026-04-13". The ISO 8601-2 notation for this ("2026-103") is
rejected instead of read, since it is easily read as a mistake for
"2026-10-03".
A week of the year, e.g. "the 5th week of 2026" becomes the range
"2026-01-26..2026-02-01". Weeks follow the ISO 8601 rule: a week starts
on a Monday, and week 1 is the week holding the first Thursday of the year,
so week 1 can start in December of the previous year. The ISO 8601-2 week
date is read too, as "2026-W05" or, for one day within the week,
"2026-W05-3", but it is never written back in that notation.
Seasons, e.g. "summer 2026" becomes the month range
"2026-06..2026-08". These are the northern-hemisphere meteorological
seasons, spring running from March to May and winter from December into
February of the following year. The EDTF season codes ("2026-22") are
rejected instead of read, being easily read as a mistake for a date.
French prose, with lang = "fr". The same conventions are read,
e.g. "vers 1910" becomes "~1910", "le XIXe siècle" becomes
"18XX", and "du 13 au 15 février 1977" becomes
"1977-02-13..1977-02-15". Accents may be left out.
Spelled-out numbers are read in English only.
A comma-separated list of dates in prose, e.g.
"13th Feb, 1977, Feb 15 1977, 1910", is split into separate dates
(here, three): a fragment that is only a year is treated as completing
the date before it.
{messydates} stores years in the ISO 8601-2 (proleptic Gregorian)
astronomical numbering, in which a year zero exists and equals 1 BCE, -0001
equals 2 BCE, and so on. Historical "BC"/"BCE" prose uses the older
convention that has no year zero, so it is converted on input: a historical
year N BCE becomes the astronomical year -(N-1), e.g. "1 BCE" becomes
"0000", "2 BCE" becomes "-0001", and "44 BCE" becomes "-0043".
A year written directly in signed ISO form (e.g. "-0044") is already
astronomical and is left unchanged, so as_messydate("-0044") (astronomical
year -44, i.e. 45 BCE) and as_messydate("44 BCE") ("-0043") intentionally
differ. "AD"/"CE" prose is simply dropped, the year being unchanged.
Era prose is a matter of input only: it is always removed on parsing, the
era of a date in an mdate being carried by the sign of its year alone.
Where it appears in the input, it is resolved for each year separately. A
marker written before a date governs that date ("{BC1044-03-15,BC1033}");
otherwise a year takes the era of the first marker written after it, so a
marker given once at the end still applies to every bound of a range or set:
"200..100 BC", "..200 BC" and "200 BC.." are all wholly BCE, whereas
"200 BC..100 AD" spans the two eras and gives "-0199..0100".
Other coerce:
coerce_from
as_messydate("2021")
as_messydate("2021-02")
as_messydate("2021-02-01")
as_messydate("01-02-2021")
as_messydate("1 February 2021")
as_messydate("First of February, two thousand and twenty-one")
as_messydate("2021-02-01?")
as_messydate("2021-02-01~")
as_messydate("2021-02-01%")
as_messydate("2021-02-01..2021-02-28")
as_messydate("{2021-02-01,2021-02-28}")
as_messydate(c("-2021", "2021 BC", "-2021-02-01"))
as_messydate(c("210201", "20210201"), resequence = "ymd")
as_messydate(c("010221", "01022021"), resequence = "dmy")
# two-digit years leave the century unspecified, unless a pivot is given
as_messydate(c("1/1/29", "May-97"))
as_messydate(c("1/1/29", "May-97"), pivot = 1930)
# serial day counts from spreadsheets need their origin
as_messydate(c(36892, 45000.5), origin = "excel")
as_messydate(c(35430, 1462), origin = "excel1904")
as_messydate(c(2021, 20210201))
# as_messydate(c("01-02-21", "01-02-2021", "01-02-91", "01-02-1991"),
# resequence = "interactive")
# ISO 8601-2 times, with the same annotations available on time components
as_messydate("2019-03-01 14:30:00Z")
as_messydate("2019-03-01 2:30pm")
as_messydate("2019-03-01 ~14:30")
# a time of day may also be given on its own, with no date part
as_messydate("2:30pm")
as_messydate("around 2pm")
# historical prose (see the "Parsing historical prose" section below)
as_messydate("MDCCLXXVI")
as_messydate("the Ides of March, 44 BC")
as_messydate("possibly about 1910")
as_messydate("the 1920s")
as_messydate("the 19th century")
as_messydate("the 103rd day of 2026")
as_messydate("the 5th week of 2026")
as_messydate("2026-W05")
as_messydate("summer 2026")
as_messydate("before 1910")
as_messydate("between the 13th and 15th of Feb, 1977")
# French prose
as_messydate(c("vers 1910", "le 15 mars 2019", "les ann\u00e9es 1990"), lang = "fr")
as_messydate("les ides de mars, 44 av. J.-C.", lang = "fr")
as_messydate(list(c("2012-06-01", "2012-06-02", "2012-06-03")))
as_messydate(list(c("2012-06-01", "2012-06-02", "2012-06-03",
"{2012-06-01, 2012-06-02, 2012-06-03}", "2012-06-01", "2012-06-03")))
# the shortest form of a messy date
as_messydate(compatible_dates(as_messydate("2012-06-01..2012-06-30")))
as_messydate(compatible_dates(as_messydate("2012-06-01..2012-06-30")),
collapse = FALSE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.