These 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 from text,
though this is a work-in-progress and currently only works in English.
Usage
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)
# S3 method for class 'numeric'
as_messydate(x, resequence = NULL)
# S3 method for class 'list'
as_messydate(x, resequence = FALSE)
mdate(x, resequence = FALSE)
Arguments
- x
A scalar or vector of a class that can be coerced into
mdate
, such asDate
,POSIXct
,POSIXlt
, or character.- resequence
Users have the option to choose the order for ambiguous dates with or without separators (e.g. "11-01-12" or "20112112").
NULL
by default. Other options include: 'dmy', 'ymd', 'mdy', 'ym', 'my' and 'interactive' If 'dmy', dates are converted from DDMMYY format for 6 digit dates, or DDMMYYYY format for 8 digit dates. If 'ymd', dates are converted from YYMMDD format for 6 digit dates, or YYYYMMDD format for 8 digit dates. If 'mdy', dates are converted from MMDDYY format for 6 digit dates or MMDDYYYY format for 8 digit dates. For these three options, ambiguous dates are converted to YY-MM-DD format for 6 digit dates, or YYYY-MM-DD format for 8 digit dates. If 'my', ambiguous 6 digit dates are converted from MM-YYYY format to YYYY-MM. If 'ym', ambiguous 6 digit dates are converted to YYYY-MM format. If 'interactive', it prompts users to select the existing component order of ambiguous dates, based on which the date is reordered into YYYY-MM-DD format and further completed to YYYY-MM-DD format if they choose to do so.
Functions
as_messydate()
: Coremdate
class coercion functionas_messydate(Date)
: Coerce fromDate
tomdate
classas_messydate(POSIXct)
: Coerce fromPOSIXct
tomdate
classas_messydate(POSIXlt)
: Coerce fromPOSIXlt
tomdate
classas_messydate(character)
: Coerce character date objects tomdate
classas_messydate(numeric)
: Coerce numeric objects tomdate
classas_messydate(list)
: Coerce list date objects to the most concise representation ofmdate
class
Examples
as_messydate("2021")
#> 'mdate' chr "2021"
as_messydate("2021-02")
#> 'mdate' chr "2021-02"
as_messydate("2021-02-01")
#> 'mdate' chr "2021-02-01"
as_messydate("01-02-2021")
#> 'mdate' chr "2021-02-01"
as_messydate("1 February 2021")
#> 'mdate' chr "2021-02-01"
as_messydate("First of February, two thousand and twenty-one")
#> 'mdate' chr "2021-02-01"
as_messydate("2021-02-01?")
#> 'mdate' chr "2021-02-01?"
as_messydate("2021-02-01~")
#> 'mdate' chr "2021-02-01~"
as_messydate("2021-02-01%")
#> 'mdate' chr "2021-02-01%"
as_messydate("2021-02-01..2021-02-28")
#> 'mdate' chr "2021-02-01..2021-02-28"
as_messydate("{2021-02-01,2021-02-28}")
#> 'mdate' chr "{2021-02-01,2021-02-28}"
as_messydate(c("-2021", "2021 BC", "-2021-02-01"))
#> 'mdate' chr [1:3] "-2021" "-2021" "-2021-02-01"
as_messydate(c("210201", "20210201"), resequence = "ymd")
#> 'mdate' chr [1:2] "0021-02-01" "2021-02-01"
as_messydate(c("010221", "01022021"), resequence = "dmy")
#> 'mdate' chr [1:2] "0021-02-01" "2021-02-01"
# as_messydate(c("01-02-21", "01-02-2021", "01-02-91", "01-02-1991"),
# resequence = "interactive")
as_messydate(list(c("2012-06-01", "2012-06-02", "2012-06-03")))
#> [[1]]
#> 'mdate' chr "2012-06-01..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")))
#> [[1]]
#> 'mdate' chr "{2012-06-01..2012-06-03,2012-06-01..2012-06-03,2012-06-01,2012-06-03}"
#>