Learn R Programming

sjmisc (version 1.2)

as_labelled: Convert vector to labelled class

Description

Convert vector to labelled class

Usage

as_labelled(x, add.labels = FALSE, add.class = 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 c

Value

  • x, as labelled-class object, including missing-flags (is_na-attribute).

Examples

Run this code
data(efc)
str(efc$e42dep)

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

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

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

data(efc)
x <- set_labels(efc$e42dep, 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)

Run the code above in your browser using DataLab