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

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

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

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

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

## 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.

A precise value needs no FUN. If x holds an imprecise value and you do not give FUN, the function uses vmin(), the earliest possible date, and states this rule in a message once in each session. In messydates 1.3.0, the message becomes a warning. In messydates 1.4.0, it becomes an error: then you must give FUN to convert an imprecise value.

...

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.double() converts x to the number of days since 1970-01-01, as as.double(as.Date(x, FUN)) does. An imprecise value is first resolved with FUN, as for as.Date().

{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(), resolve_probabilistically()

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)
# "1000 BC" is the astronomical year -0999 (year zero exists), whereas the
# signed ISO "-1000" below is astronomical year -1000
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.double(as_messydate("2012-01-01"))
as.double(as_messydate("2012-01"), FUN = vmax)

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