defined: Create a semantically well-defined, labelled vector

View source: R/defined.R

definedR Documentation

Create a semantically well-defined, labelled vector

Description

Creates a semantically well-defined vector enriched with metadata. defined() is an S3 constructor that extends numeric or character vectors with a human-readable label, unit of measurement, linked concept, and optional namespace. These objects preserve semantics while behaving like standard vectors in comparisons, printing, and subsetting. The defined constructor creates the objects of this class, which are semantically extended vectors inherited from haven::labelled.

Usage

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

is.defined(x)

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

Arguments

x

A vector to label. Must be either numeric (integer or double) or character.

labels

A named vector or NULL. The vector should be the same type as x. Unlike factors, labels don't need to be exhaustive: only a fraction of the values might be labelled.

label

A short, human-readable description of the vector or NULL.

unit

A character string of length one containing the unit of measure or NULL.

concept

A character string of length one containing a linked concept or NULL.

namespace

A namespace for individual observations or categories or NULL.

...

Further parameters for inheritance, not in use.

object

An R object to be summarised.

Details

A defined vector is an extension of a base vector with additional semantic metadata:

  • A label (label): a short human-readable description

  • A unit (unit): e.g., "kg", "hours", "USD"

  • A concept (concept): a URI or textual reference

  • A namespace (namespace): for URI-based observation or value identifiers

The class inherits from haven::labelled, supports typical vector operations (subsetting, comparisons, printing), and integrates with tibbles and tidy workflows via custom format(), print(), and as.vector() methods.

Use is.defined() to test if an object is of class defined. Use as_numeric() and as_character() to coerce to base types.

Value

The constructor defined returns a vector with defined value labels, a variable label, an optional unit of measurement and linked concept.
is.defined returns a logical value, stating if the object is of class defined.

See Also

Other defined metadata methods and functions: var_label(), var_namespace(), var_unit()

Examples


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

# To check the s3 class of the vector:
is.defined(gdp_vector)

# To print the defined vector:
print(gdp_vector)

# To summarise the defined vector:
summary(gdp_vector)

# Subsetting work as expected:
gdp_vector[1:2]

dataset documentation built on June 8, 2025, 10:15 a.m.