| as_character | R Documentation |
as_character() is the recommended method to convert a
defined() vector into a character vector. It is metadata-aware
and provides explicit control over whether semantic attributes are preserved.
Base R as.character() always strips the class and metadata.
as_character(x, ...)
## S3 method for class 'haven_labelled_defined'
as_character(x, strip_attributes = TRUE, ...)
## S3 method for class 'haven_labelled_defined'
as.character(x, ...)
x |
A vector created with |
... |
Reserved for potential future use. |
strip_attributes |
Logical; should semantic metadata attributes
(such as |
If preserve_attributes = TRUE, the returned character vector retains
metadata attributes (unit, concept, namespace, label). The
"defined" class is always removed.
If preserve_attributes = FALSE (default), a plain character vector is
returned with all metadata stripped.
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.character() always drops all attributes and returns plain
character values. It is equivalent to:
as_character(x, strip_attributes = TRUE).
A character vector (plain or with attributes).
x <- defined(c("apple", "banana"), label = "Fruit", unit = "kg")
# Recommended:
as_character(x)
# Preserve metadata:
as_character(x, strip_attributes = FALSE)
# Base R:
as.character(x)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.