Learn R Programming

dataset (version 0.4.1)

as.Date.haven_labelled_defined: Coerce a defined Date vector to a base R Date

Description

Coerces a haven_labelled_defined vector whose underlying type is Date into a base R Date vector.

This method preserves the underlying date values and, by default, also retains any semantic metadata attached to the variable.

Usage

# S3 method for haven_labelled_defined
as.Date(x, strip_attributes = FALSE, ...)

Value

A Date vector, optionally carrying semantic metadata.

Arguments

x

A vector created with defined() with underlying type Date.

strip_attributes

Logical; should the semantic metadata attributes (label, unit, definition, namespace) be removed from the returned vector? Defaults to FALSE.

...

Additional arguments passed to base::as.Date().

Details

Use strip_attributes = TRUE when flattening or preparing data for external pipelines, but keep the default when working with defined vectors directly.

Base R's as.Date() also works, as it dispatches to this method via S3. However, using as.Date() on defined vectors is considered safe because this method ensures metadata is handled predictably.

See Also

as.POSIXct(), as_numeric(), as_character(), as_logical(), defined()

Examples

Run this code
d <- defined(Sys.Date() + 0:2, label = "Observation date")

# Recommended usage
as.Date(d)

# Stripping metadata
as.Date(d, strip_attributes = TRUE)

Run the code above in your browser using DataLab