Converts factors, character vectors, or plain numeric vectors to
haven_labelled class, optionally assigning value labels and a variable
label. This is the reverse of to_label().
Arguments
- data
A data frame, tibble, or a single vector.
- ...
Optional: unquoted variable names (tidyselect supported). If empty on a data frame, converts all factor columns.
- labels
Optional: a named numeric vector of value labels to assign. If
NULLand the input is a factor, factor levels are used as labels.- label
Optional: a variable label string.
Value
The input with variables converted to haven_labelled. For
single vector input, returns a haven_labelled vector.
Details
See also
to_label() for the reverse operation, val_labels() for
setting labels on existing labelled vectors
Other labels:
copy_labels(),
drop_labels(),
find_var(),
set_na(),
to_character(),
to_label(),
to_numeric(),
unlabel(),
val_labels(),
var_label()
Examples
# Factor → haven_labelled
x <- factor(c("Male", "Female", "Male"))
to_labelled(x)
#> <labelled<double>[3]>
#> [1] 2 1 2
#>
#> Labels:
#> value label
#> 1 Female
#> 2 Male
# Numeric with custom labels
to_labelled(c(1, 2, 3),
labels = c("Low" = 1, "Medium" = 2, "High" = 3),
label = "Satisfaction level"
)
#> <labelled<double>[3]>: Satisfaction level
#> [1] 1 2 3
#>
#> Labels:
#> value label
#> 1 Low
#> 2 Medium
#> 3 High
# All factors in a data frame
data <- to_labelled(survey_data)
