haven (version 0.1.1)

labelled: Create a labelled vector.

Description

A labelled vector is a common data structure in other statistical environments. This class makes it possible to import such labelled vectors in to without loss of fidelity. This class provides few methods, as I expect you'll coerce to a standard R class (e.g. a factor) soon after importing.

Usage

labelled(x, labels)

## S3 method for class 'labelled': as_factor(x, levels = c("labels", "values"), ordered = FALSE, ...)

zap_labels(x)

Arguments

x
A vector to label. Must be either numeric (integer or double) or character.
labels
A 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.
levels
When coercing a labelled character vector to a factor, you can choose whether to use the labels or the values as the factor levels.
ordered
If TRUE for ordinal factors, FALSE (the default) for nominal factors.
...
Ignored

Examples

Run this code
s1 <- labelled(c("M", "M", "F"), c(Male = "M", Female = "F"))
s2 <- labelled(c(1, 1, 2), c(Male = 1, Female = 2))

# Unfortunately it's not possible to make as.factor work for labelled objects
# so instead use as_factor
as_factor(s1)
as_factor(s1, labels = "values")
as_factor(s2)

# Often when you have a partially labelled numeric vector, labelled values
# are special types of missing. Use XXX to replace labels with missing
# values
x <- labelled(c(1, 2, 1, 2, 10, 9), c(Unknown = 9, Refused = 10))
zap_labels(x)

Run the code above in your browser using DataCamp Workspace