Learn R Programming

dataset (version 0.4.1)

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

Description

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

This method preserves both the timestamp values and the original time zone. By default, semantic metadata is also retained.

Usage

# S3 method for haven_labelled_defined
as.POSIXct(x, tz = "", strip_attributes = TRUE, ...)

Value

A POSIXct vector with timestamp values preserved.

Arguments

x

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

tz

a character string. The time zone specification to be used for the conversion, if one is required. System-specific timezones (see base::timezones(), but "" is the current time zone, and "GMT" is UTC (Universal Time, Coordinated). Invalid values are most commonly treated as UTC, on some platforms with a warning.

strip_attributes

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

...

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

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.POSIXct() also works, as it dispatches to this method via S3. Using this method directly is preferred when metadata preservation matters.

See Also

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

Examples

Run this code
p <- defined(
  as.POSIXct("2024-01-01 12:00:00", tz = "UTC"),
  label = "Timestamp"
)

# Recommended usage
as.POSIXct(p)

# Explicit attribute stripping
as.POSIXct(p, strip_attributes = TRUE)

Run the code above in your browser using DataLab