coerce_from: Coercion from 'mdate' to common date classes

coerce_fromR Documentation

Coercion from mdate to common date classes

Description

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 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, ...)

## S3 method for class 'mdate'
as.data.frame(x, ...)

## S3 method for class 'mdate'
as.list(x, ...)

## S3 method for class 'mdate'
as.double(x, ...)

## S4 method for signature 'mdate'
as_datetime(x, ...)

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(). vmin(), vmax(), vmean(), vmedian(), vmodal(), and vrandom() are the vectorised equivalents, resolving each element separately rather than summarising the whole vector.

...

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.

Details

as.Date() always drops any time of day carried by x (a calendar date has no time component); use as.POSIXct() or as.POSIXlt() to keep the time.

as.POSIXct() and as.POSIXlt() keep the time of day (defaulting to midnight if x is date-only), and honour a UTC offset if x carries one. They do not support dates before the common era; use as.Date() for those.

as.data.frame() places the (unresolved) mdate vector in a single-column data frame, as for any other vector.

as.list() splits x into a list of length-one mdate objects, one per element, without resolving any of them.

as.double() converts x to the number of days since 1970-01-01 (as for as.double(as.Date(x))), without resolving ranges, sets, or unspecified components first; it is mostly useful for already-precise dates.

{lubridate}'s as_date() and as_datetime() also accept an mdate (delegating to as.Date()/as.POSIXct() above, so the FUN resolver still applies).

Value

A date object of Date, POSIXct, or POSIXlt class

See Also

resolve_extrema(), resolve_tendency()

Other coerce: coerce_to

Examples

as.Date(as_messydate("2012-01"), FUN = vmin)
as.Date(as_messydate("2012-01-01"), FUN = vmean)
as.Date(as_messydate("2012-01"), FUN = vmax)
as.Date(as_messydate("2012-01"), FUN = vmedian)
as.Date(as_messydate("2012-01"), FUN = vmodal)
as.Date(as_messydate("2012-01"), FUN = vrandom)
as.Date(as_messydate("1000 BC"), FUN = vmax)
as.Date(as_messydate("1000 BC"), FUN = vmedian)
as.Date(as_messydate(c("-1000", "2020")), FUN = vmin)
# the time of day, if any, is dropped
as.Date(as_messydate("2012-01-01 14:30"), FUN = vmin)
as.POSIXct(as_messydate("2012-01-01 14:30:00"), FUN = vmin)
as.POSIXct(as_messydate("2012-01-01 14:30:00+02:00"), FUN = vmin)
as.POSIXlt(as_messydate("2012-01-01 14:30:00"), FUN = vmin)
as.data.frame(as_messydate(c("2012-01-01", "2012-02")))
as.list(as_messydate(c("2012-01-01", "2012-02")))
as.double(as_messydate("2012-01-01"))

messydates documentation built on July 17, 2026, 1:07 a.m.