This function converts (replaces) values of a variable (also of factors or character vectors) with their associated value labels. Might be helpful for factor variables. For instance, if you have a Gender variable with 0/1 value, and associated labels are male/female, this function would convert all 0 to male and all 1 to female and returns the new variable as factor.
to_label(
x,
...,
add.non.labelled = FALSE,
prefix = FALSE,
var.label = NULL,
drop.na = TRUE,
drop.levels = FALSE
)
A vector or data frame.
Optional, unquoted names of variables that should be selected for
further processing. Required, if x
is a data frame (and no
vector) and only selected variables from x
should be processed.
You may also use functions like :
or tidyselect's
select_helpers
.
See 'Examples' or package-vignette.
Logical, if TRUE
, values without associated
value label will also be converted to labels (as is). See 'Examples'.
Logical, if TRUE
, the value labels used as factor levels
or character values will be prefixed with their associated values. See 'Examples'.
Optional string, to set variable label attribute for the
returned variable (see vignette Labelled Data and the sjlabelled-Package).
If NULL
(default), variable label attribute of x
will
be used (if present). If empty, variable label attributes will be removed.
Logical, if TRUE
, tagged NA
values with value labels
will be converted to regular NA's. Else, tagged NA
values will be replaced
with their value labels. See 'Examples' and get_na
.
Logical, if TRUE
, unused factor levels will be
dropped (i.e. droplevels
will be applied before returning
the result).
A factor with the associated value labels as factor levels. If x
is a data frame, the complete data frame x
will be returned,
where variables specified in ...
are coerced to factors;
if ...
is not specified, applies to all variables in the
data frame.
# NOT RUN {
library(sjlabelled)
data(efc)
print(get_labels(efc)['c161sex'])
head(efc$c161sex)
head(to_label(efc$c161sex))
# Find more examples at '?sjlabelled::as_label'
# }
Run the code above in your browser using DataLab