Converts a (labelled) vector of any class into a labelled
class vector, resp. adds a labelled class-attribute.
as_labelled(
x,
add.labels = FALSE,
add.class = FALSE,
skip.strings = FALSE,
tag.na = FALSE
)Variable (vector), data.frame or list of variables
that should be converted to labelled()-class
objects.
Logical, if TRUE, non-labelled values will be
labelled with the corresponding value.
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.
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.
Logical, if TRUE, tagged NA values are replaced
by their associated values. This is required, for instance, when writing
data back to SPSS.
x, as labelled-class object.
data(efc)
str(efc$e42dep)
#> num [1:908] 3 3 3 4 4 4 4 4 4 4 ...
#> - attr(*, "label")= chr "elder's dependency"
#> - attr(*, "labels")= Named num [1:4] 1 2 3 4
#> ..- attr(*, "names")= chr [1:4] "independent" "slightly dependent" "moderately dependent" "severely dependent"
x <- as_labelled(efc$e42dep)
str(x)
#> dbl+lbl [1:908] 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 3, 4, 3, 3, 3, 1, 3, 3, 4...
#> @ label : chr "elder's dependency"
#> @ labels: Named num [1:4] 1 2 3 4
#> ..- attr(*, "names")= chr [1:4] "independent" "slightly dependent" "moderately dependent" "severely dependent"
x <- as_labelled(efc$e42dep, add.class = TRUE)
str(x)
#> numeric [1:908] 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 3, 4, 3, 3, 3, 1, 3, 3, 4...
#> @ label : chr "elder's dependency"
#> @ labels: Named num [1:4] 1 2 3 4
#> ..- attr(*, "names")= chr [1:4] "independent" "slightly dependent" "moderately dependent" "severely dependent"
a <- c(1, 2, 4)
x <- as_labelled(a, add.class = TRUE)
str(x)
#> num [1:3] 1 2 4
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)
#> dbl+lbl [1:908] 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 3, 4, 3, 3, 3, 1, 3, 3, 4...
#> @ label : chr "elder's dependency"
#> @ labels: Named num [1:2] 1 4
#> ..- attr(*, "names")= chr [1:2] "independent" "severe dependency"
str(x2)
#> dbl+lbl [1:908] 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 3, 4, 3, 3, 3, 1, 3, 3, 4...
#> @ label : chr "elder's dependency"
#> @ labels: Named num [1:4] 1 2 3 4
#> ..- attr(*, "names")= chr [1:4] "independent" "2" "3" "severe dependency"
get_values(x1)
#> [1] 1 4
get_values(x2)
#> [1] 1 2 3 4