| Title: | Lightweight Variable Labels |
|---|---|
| Description: | Assign, extract, or remove variable labels from R vectors. Lightweight and dependency-free. |
| Authors: | Marius Barth [aut, cre] (ORCID: <https://orcid.org/0000-0002-3421-6665>) |
| Maintainer: | Marius Barth <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.2.5.9000 |
| Built: | 2026-06-09 07:16:04 UTC |
| Source: | https://github.com/mariusbarth/tinylabels |
Functions to convert labelled vectors to other types, possibly keeping the variable
label and the class attribute tiny_labelled.
## S3 method for class 'tiny_labelled' as.character(x, keep_label = TRUE, ...) ## S3 method for class 'tiny_labelled' as.logical(x, keep_label = TRUE, ...) ## S3 method for class 'tiny_labelled' as.integer(x, keep_label = TRUE, ...) ## S3 method for class 'tiny_labelled' as.double(x, keep_label = TRUE, ...) ## S3 method for class 'tiny_labelled' as.complex(x, keep_label = TRUE, ...)## S3 method for class 'tiny_labelled' as.character(x, keep_label = TRUE, ...) ## S3 method for class 'tiny_labelled' as.logical(x, keep_label = TRUE, ...) ## S3 method for class 'tiny_labelled' as.integer(x, keep_label = TRUE, ...) ## S3 method for class 'tiny_labelled' as.double(x, keep_label = TRUE, ...) ## S3 method for class 'tiny_labelled' as.complex(x, keep_label = TRUE, ...)
x |
Object to be coerced |
keep_label |
Logical indicating whether the variable labels and class |
... |
Further arguments passed to methods |
label_variable() can be used to assign variable labels within a workflow
using the tidyverse's pipe operator.
label_variable(x, ...) label_variables(x, ...)label_variable(x, ...) label_variables(x, ...)
x |
Either a vector or a |
... |
Variable label(s) to be assigned. For data frames, these have to be name-value pairs, see example. |
library(dplyr) test <- npk %>% label_variable(N = "Nitrogen", P = "Phosphate") variable_label(test)library(dplyr) test <- npk %>% label_variable(N = "Nitrogen", P = "Phosphate") variable_label(test)
The levels of a factor are re-ordered so that the level specified by ref is
first and the others are moved down. This is a copy from relevel
in the stats package, but preserves the label attribute and class tiny_labelled.
## S3 method for class 'tiny_labelled' relevel(x, ref, ...)## S3 method for class 'tiny_labelled' relevel(x, ref, ...)
x |
an unordered factor. |
ref |
the reference level, typically a string. |
... |
additional arguments for future methods. |
Remove variable_labels from a labelled vector or from the columns of a data frame.
unlabel(x)unlabel(x)
x |
An R object. |
Object as x but without variable labels and with class tiny_labelled removed.
Assign or extract variable labels of a vector or
the columns (i.e., vectors) of a data.frame.
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) <- valuevariable_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
x |
Either a vector or a |
... |
Further arguments that may be passed to methods. |
value |
Character. The variable label(s) to be assigned. If |
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 label_variable() for an alternative that is compatible with the tidyverse's pipe operator.
# 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# 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