variable_label {tinylabels}R Documentation

Assign or Extract Variable Labels

Description

Assign or extract variable labels of a vector or the columns (i.e., vectors) of a data.frame.

Usage

variable_label(x, ...)

## Default S3 method:
variable_label(x, ...)

## S3 method for class 'data.frame'
variable_label(x, ...)

variable_label(x) <- value

## Default S3 replacement method:
variable_label(x) <- value

## S3 replacement method for class 'data.frame'
variable_label(x) <- value

variable_labels(x, ...)

variable_labels(x) <- value

Arguments

x

Either a vector or a data.frame.

...

Further arguments that may be passed to methods.

value

Character. The variable label(s) to be assigned. If variable_label() is applied to a single vector, this should be a length-one argument. If applied to a data.frame, value is required to be a named vector or a named list. NULL elements of this name-value list are ignored. Check the examples for details.

Value

For vectors, variable_label() returns NULL or the variable label (typically of length one). For data frames, variable_label() returns a named list where each column corresponds to a column of the data frame.

The assignment methods ⁠variable_label()<-⁠ return the labelled object.

See Also

See label_variable() for an alternative that is compatible with the tidyverse's pipe operator.

Examples

  # label a single vector
  variable_label(letters) <- "The alphabet" # Assign
  variable_label(letters)                   # Extract

  # label some columns of a data frame:
  variable_labels(npk) <- c(                # Assign
    N = "Nitrogen"
    , P = "Phosphate"
    , K = "Potassium"
  )
  variable_labels(npk)                      # Extract

  # using a list on the right, character and expression can be mixed:
  variable_labels(npk) <- list(             # Assign
    N = "Nitrogen"
    , P = "Phosphate"
    , K = expression(italic(K))
  )
  variable_labels(npk)                      # Extract

[Package tinylabels version 0.2.4 Index]