Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

as_labelled

Convert vector to labelled class


Description

Converts a (labelled) vector of any class into a labelled class vector, resp. adds a labelled class-attribute.

Usage

as_labelled(
  x,
  add.labels = FALSE,
  add.class = FALSE,
  skip.strings = FALSE,
  tag.na = FALSE
)

Arguments

x

Variable (vector), data.frame or list of variables that should be converted to labelled()-class objects.

add.labels

Logical, if TRUE, non-labelled values will be labelled with the corresponding value.

add.class

Logical, if TRUE, x preserves its former class-attribute and labelled is added as additional attribute. If FALSE (default), all former class-attributes will be removed and the class-attribute of x will only be labelled.

skip.strings

Logical, if TRUE, character vector are not converted into labelled-vectors. Else, character vectors are converted to factors vector and the associated values are used as value labels.

tag.na

Logical, if TRUE, tagged NA values are replaced by their associated values. This is required, for instance, when writing data back to SPSS.

Value

x, as labelled-class object.

Examples

data(efc)
str(efc$e42dep)

x <- as_labelled(efc$e42dep)
str(x)

x <- as_labelled(efc$e42dep, add.class = TRUE)
str(x)

a <- c(1, 2, 4)
x <- as_labelled(a, add.class = TRUE)
str(x)

data(efc)
x <- set_labels(efc$e42dep,
                labels = c(`1` = "independent", `4` = "severe dependency"))
x1 <- as_labelled(x, add.labels = FALSE)
x2 <- as_labelled(x, add.labels = TRUE)

str(x1)
str(x2)

get_values(x1)
get_values(x2)

sjlabelled

Labelled Data Utility Functions

v1.1.8
GPL-3
Authors
Daniel Lüdecke [aut, cre] (<https://orcid.org/0000-0002-8895-3206>), avid Ranzolin [ctb], Jonathan De Troye [ctb]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.