| as_logical | R Documentation |
Coerces a haven_labelled_defined vector created with defined()
into a base R logical vector.
This function is the recommended, semantic-aware interface for converting defined logical vectors. It preserves the underlying logical values and, unless otherwise requested, retains the semantic metadata attached to the variable.
as_logical(x, ...)
## S3 method for class 'haven_labelled_defined'
as_logical(x, strip_attributes = TRUE, ...)
x |
A vector created with |
... |
Additional arguments (currently unused). |
strip_attributes |
Logical; should semantic metadata attributes
(such as |
Use strip_attributes = TRUE when flattening or preparing data for
external pipelines, but keep the default when working with defined
vectors directly.
Users may also call base R's as.logical(), which will dispatch to this
method automatically via S3. However, as_logical() is preferred because
it makes the semantics explicit and supports the strip_attributes
argument.
A logical vector. If strip_attributes = FALSE, any semantic
metadata attached to x will be carried over to the returned vector.
as_numeric(), as_character(),
as.Date(), as.POSIXct(),
defined()
# Basic usage
flg <- defined(c(TRUE, FALSE, TRUE), label = "Flag")
as_logical(flg)
# Metadata preserved by default
attr(as_logical(flg), "label")
# Stripping metadata
as_logical(flg, strip_attributes = TRUE)
# Base R coercion also works (via S3 method dispatch)
as.logical(flg)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.