Title: | Label Attributes for Atomic Vectors |
---|---|
Description: | Labels are a common construct in statistical software providing a human readable description of a variable. While variable names are succinct, quick to type, and follow a language's naming conventions, labels may be more illustrative and may use plain text and spaces. R does not provide native support for labels. Some packages, however, have made this feature available. Most notably, the 'Hmisc' package provides labelling methods for a number of different object. Due to design decisions, these methods are not all exported, and so are unavailable for use in package development. The 'labelVector' package supports labels for atomic vectors in a light-weight design that is suitable for use in other packages. |
Authors: | Benjamin Nutter [aut, cre] |
Maintainer: | Benjamin Nutter <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.2 |
Built: | 2025-02-10 03:13:26 UTC |
Source: | https://github.com/cran/labelVector |
Extraction and replacement methods for labelled vectors.
## S3 method for class 'labelled' x[i, ...] ## S3 replacement method for class 'labelled' x[i, ...] <- value
## S3 method for class 'labelled' x[i, ...] ## S3 replacement method for class 'labelled' x[i, ...] <- value
x |
An atomic vector inheriting the |
i |
The elements to extract. |
... |
Arguments to pass to other methods. |
value |
typically a vector of similar class of length |
x <- set_label(1:10, "Integers") x[1:3] x[3] <- pi x
x <- set_label(1:10, "Integers") x[1:3] x[3] <- pi x
Retrieve the label attribute of a labelled vector. If the vector has no label, the vector name is returned as a string.
get_label(x, ...) ## Default S3 method: get_label(x, ...) ## S3 method for class 'data.frame' get_label(x, vars = NULL, ..., return_vector = TRUE)
get_label(x, ...) ## Default S3 method: get_label(x, ...) ## S3 method for class 'data.frame' get_label(x, vars = NULL, ..., return_vector = TRUE)
x |
An atomic vector. |
... |
Arguments to pass to other methods. |
vars |
A character vector of variable names in |
return_vector |
|
x <- 1:10 x <- set_label(x, "Integers") get_label(x) y <- letters attr(y, "label") # NULL get_label(y) # "y" # Set labels for variables in a data frame mtcars2 <- set_label(mtcars, am = "Automatic / Manual", mpg = "Miles per Gallon", gear = "Number of gears") get_label(mtcars2)
x <- 1:10 x <- set_label(x, "Integers") get_label(x) y <- letters attr(y, "label") # NULL get_label(y) # "y" # Set labels for variables in a data frame mtcars2 <- set_label(mtcars, am = "Automatic / Manual", mpg = "Miles per Gallon", gear = "Number of gears") get_label(mtcars2)
Functions to determine if a vector has a label.
is.labelled(x) is_labelled(x)
is.labelled(x) is_labelled(x)
x |
An atomic vector |
Returns a logical(1)
.
Return a logical value of length 1.
Cast an error if x
is not atomic.
Labelled vectors are printed with their label appearing above the content of the vector.
## S3 method for class 'labelled' print(x, ...)
## S3 method for class 'labelled' print(x, ...)
x |
A vector inheriting class |
... |
Additional arguments to pass to other methods. |
Variable labels are a common construct in statistical software, giving users the ability to provide plain text descriptions for variables. These descriptions can be more informative of the variable's purpose, since they need not be restricted to the naming conventions imposed on variable names.
set_label(x, ...) ## Default S3 method: set_label(x, label, ...) ## S3 method for class 'data.frame' set_label(x, ..., .dots = list())
set_label(x, ...) ## Default S3 method: set_label(x, label, ...) ## S3 method for class 'data.frame' set_label(x, ..., .dots = list())
x |
An atomic vector |
... |
For the default method, arguments to pass to other methods.
For the |
label |
|
.dots |
for data frames, a named list of key-pairs mapping the variable name to the label. |
Frank E Harrell Jr, with contributions from Charles Dupont and many others. (2017). Hmisc: Harrell Miscellaneous. R package version 4.0-3. https://CRAN.R-project.org/package=Hmisc
x <- 1:10 x <- set_label(x, "Integers") x # Set labels for variables in a data frame mtcars2 <- set_label(mtcars, am = "Automatic / Manual", mpg = "Miles per Gallon", gear = "Number of gears") get_label(mtcars2)
x <- 1:10 x <- set_label(x, "Integers") x # Set labels for variables in a data frame mtcars2 <- set_label(mtcars, am = "Automatic / Manual", mpg = "Miles per Gallon", gear = "Number of gears") get_label(mtcars2)