Learn R Programming

dataset (version 0.4.1)

defined: Create a semantically enriched vector with variable-level metadata

Description

defined() constructs a vector that behaves like a base R vector but carries semantic metadata used for documentation, validation, and interoperability. The resulting object inherits from haven::labelled() (for numeric, character, and factor data) or from base date/time classes, and adds:

Usage

defined(
  x,
  labels = NULL,
  label = NULL,
  unit = NULL,
  concept = NULL,
  namespace = NULL,
  ...
)

is.defined(x)

# S3 method for haven_labelled_defined summary(object, ...)

Value

A vector of class "haven_labelled_defined" or "datetime_defined", depending on the input type.

Arguments

x

A vector to annotate.

labels

Optional named vector of value labels. Only supported for numeric or character vectors (not for logical).

label

A short human-readable variable label (character of length 1).

unit

Unit of measurement (character length 1) or NULL.

concept

A URI or identifier describing the meaning or definition of the variable. This replaces the deprecated definition argument.

namespace

Optional string or named character vector used to generate value-level URIs via substitution ($1 macro).

...

For backward compatibility; the deprecated definition argument is still accepted and mapped to concept.

object

An R object to be summarised.

Supported input types

  • numeric (integer or double)

  • character

  • factor (converted via labelled::to_labelled())

  • Date

  • POSIXct

  • haven::labelled()

  • logical (with restrictions: logical vectors cannot have value labels)

Details

  • a human-readable variable label,

  • an optional unit of measurement,

  • a concept URI identifying the meaning of the variable (formerly called definition),

  • an optional namespace used for value-level URI expansion,

  • optional labelled values (where supported).

The concept attribute is a general semantic anchor and may refer to: a measure or dimension concept (SDMX-style), a property IRI (e.g. Wikibase), or any URI that defines or describes the variable.

defined() vectors preserve their metadata during subsetting, printing, summarizing, comparisons, and many tidyverse operations. They integrate smoothly with dataset_df() objects and can be safely flattened via as.data.frame(), as_tibble(), or coercion helpers such as as_numeric() and as_character().

See Also

is.defined(), as_numeric(), as_character(), as_logical(), strip_defined(), dataset_df() print.haven_labelled_defined()

Examples

Run this code
gdp_vector <- defined(
  c(3897, 7365, 6753),
  label = "Gross Domestic Product",
  unit = "million dollars",
  concept = "http://data.europa.eu/83i/aa/GDP"
)

is.defined(gdp_vector)
print(gdp_vector)
summary(gdp_vector)
gdp_vector[1:2]

Run the code above in your browser using DataLab