as.numeric.haven_labelled_defined | R Documentation |
Base R's as.numeric()
does not support custom classes like
defined
. Calling as.numeric()
on a defined
vector will drop all
metadata and class information, which equals to
as_numeric(x, preserve_attributes = FALSE)
.
as_numeric()
is the recommended method to convert a defined
vector to numeric. It is metadata-aware and ensures that the underlying data
is numeric before coercion.
## S3 method for class 'haven_labelled_defined'
as.numeric(x, ...)
as_numeric(x, ...)
## S3 method for class 'haven_labelled_defined'
as_numeric(x, preserve_attributes = FALSE, ...)
x |
A vector created with |
... |
Further arguments passed to internal methods (not used). |
preserve_attributes |
Defaults to |
as_numeric()
allows preserve_attributes = TRUE
when the
resulting vector will retain relevant metadata such as the unit
,
concept
, and namespace
attributes, but it will no longer be of class
defined
. If preserve_attributes = FALSE
(default), a plain numeric
vector is returned with all metadata and class dropped.
For
character-based defined
vectors, as_numeric()
will throw an informative
error to prevent accidental coercion of non-numeric data.
A numeric vector.
as_numeric
strip_defined()
as_numeric(orange_df$age, preserve_attributes = TRUE)
gdp <- defined(c(3897L, 7365L), label = "GDP", unit = "million dollars")
gdp_numbers <- as_numeric(gdp)
gdp_numbers
attributes(gdp_numbers)
gdp_striped <- as_numeric(gdp, preserve_attributes = FALSE)
attributes(gdp_striped)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.