as_character: Coerce a defined vector to character

View source: R/as_character.R

as_characterR Documentation

Coerce a defined vector to character

Description

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.

Usage

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

Arguments

x

A vector created with defined().

...

Reserved for potential future use.

strip_attributes

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

Details

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

Value

A character vector (plain or with attributes).

Examples

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)


dataset documentation built on Nov. 16, 2025, 5:06 p.m.