Learn R Programming

sjmisc (version 1.8)

labelled: Create a labelled vector

Description

A labelled vector is a common data structure in other statistical environments.

Usage

labelled(x, labels, is_na = NULL)

Arguments

x
Vector to label. Must be either numeric or character.
labels
Named vector. 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.
is_na
Optionally, logical vector describing which levels should be translated to missing values

Examples

Run this code
# labelled vector with multiple types of missing values
x <- labelled(c("M", "M", "F", "X", "N/A"),
              c(Male = "M", Female = "F", Refused = "X", "Not applicable" = "N/A"),
              c(FALSE, FALSE, TRUE, TRUE))

x <- labelled(c(1, 2, 1, 5, 1, 5, 9),
              c(Male = 1, Female = 2, Refused = 5, Missing = 9),
              c(FALSE, FALSE, TRUE, TRUE))

Run the code above in your browser using DataLab