
Coerce a defined POSIXct vector to a base R POSIXct
Source:R/as.POSIXct.R
as.POSIXct.haven_labelled_defined.RdCoerces 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 class 'haven_labelled_defined'
as.POSIXct(x, tz = "", strip_attributes = TRUE, ...)Arguments
- x
A vector created with
defined()with underlying typePOSIXct.- 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.
Examples
p <- defined(
as.POSIXct("2024-01-01 12:00:00", tz = "UTC"),
label = "Timestamp"
)
# Recommended usage
as.POSIXct(p)
#> [1] "2024-01-01 12:00:00 UTC"
# Explicit attribute stripping
as.POSIXct(p, strip_attributes = TRUE)
#> [1] "2024-01-01 12:00:00 UTC"