Skip to contents

These functions coerce objects of mdate class to common date classes such as Date, POSIXct, and POSIXlt. Since mdate objects can hold multiple individual dates, however, an additional function must be passed as an argument so that these functions know how to coerce resolve multiple dates into a single date.

For example, one might wish to use the earliest possible date in any ranges of dates (min), the latest possible date (max), some notion of a central tendency (mean, median, or modal), or even a random selection from among the candidate dates.

These functions then, building on expand() and the resolve functions, are particularly useful in converting back out of the mdate class for use with existing methods and models, especially for checking the robustness of results.

Usage

# S3 method for class 'mdate'
as.Date(x, FUN = vmin, ...)

# S3 method for class 'mdate'
as.POSIXct(x, tz = "UTC", FUN = vmin, ...)

# S3 method for class 'mdate'
as.POSIXlt(x, tz = "UTC", FUN = vmin, ...)

Arguments

x

A mdate object

FUN

A function that can be used to resolve expanded messy dates into a single date. For example, min(), max(), mean(), median(), modal(), and random().

...

Arguments passed on to the S3 generics.

tz

Character string specifying the time zone for the conversion, if required. By default "UTC" (Universal Time Coordinated), equivalent to GMT. If "" then the current time zone is used.

Value

A date object of Date, POSIXct, or POSIXlt class

Examples

as.Date(as_messydate("2012-01"), FUN = vmin)
#> [1] "2012-01-01"
as.Date(as_messydate("2012-01-01"), FUN = vmean)
#> [1] "2012-01-01"
as.Date(as_messydate("2012-01"), FUN = vmax)
#> [1] "2012-01-31"
as.Date(as_messydate("2012-01"), FUN = vmedian)
#> [1] "2012-01-16"
as.Date(as_messydate("2012-01"), FUN = vmodal)
#> [1] "2012-01-01"
as.Date(as_messydate("2012-01"), FUN = vrandom)
#> [1] "2012-01-13"
as.Date(as_messydate("1000 BC"), FUN = vmax)
#> [1] "-1000-12-31"
as.Date(as_messydate("1000 BC"), FUN = vmedian)
#> [1] "-1000-07-02"
as.Date(as_messydate(c("-1000", "2020")), FUN = vmin)
#> [1] "-1000-01-01" "2020-01-01"