
Add, replace or remove value labels of variables
Source:R/add_labels.R, R/remove_labels.R
add_labels.RdThese functions add, replace or remove value labels to or from variables.
Arguments
- x
A vector or data frame.
- ...
Optional, unquoted names of variables that should be selected for further processing. Required, if
xis a data frame (and no vector) and only selected variables fromxshould be processed. You may also use functions like:or tidyselect's select-helpers. See 'Examples'.- labels
- For
add_labels() A named (numeric) vector of labels that will be added to
xas label attribute.- For
remove_labels() Either a numeric vector, indicating the position of one or more label attributes that should be removed; a character vector with names of label attributes that should be removed; or a
tagged_na()to remove the labels from specific NA values.
- For
Value
x with additional or removed value labels. If x
is a data frame, the complete data frame x will be returned,
with removed or added to variables specified in ...;
if ... is not specified, applies to all variables in the
data frame.
Details
add_labels() adds labels to the existing value
labels of x, however, unlike set_labels, it
does not remove labels that were not specified in
labels. add_labels() also replaces existing
value labels, but preserves the remaining labels.
remove_labels() is the counterpart to add_labels().
It removes labels from a label attribute of x.
replace_labels() is an alias for add_labels().
See also
set_label to manually set variable labels or
get_label to get variable labels; set_labels to
add value labels, replacing the existing ones (and removing non-specified
value labels).
Examples
# add_labels()
data(efc)
get_labels(efc$e42dep)
#> [1] "independent" "slightly dependent" "moderately dependent"
#> [4] "severely dependent"
x <- add_labels(efc$e42dep, labels = c(`nothing` = 5))
#> Error in get_na_helper(x, as.tag): Package 'haven' required for this function. Please install it.
get_labels(x)
#> Error: object 'x' not found
if (require("dplyr")) {
x <- efc %>%
# select three variables
dplyr::select(e42dep, c172code, c161sex) %>%
# only add new label to two of those
add_labels(e42dep, c172code, labels = c(`nothing` = 5))
# see data frame, with selected variables having new labels
get_labels(x)
}
#> Loading required package: dplyr
#> Warning: there is no package called ‘dplyr’
x <- add_labels(efc$e42dep, labels = c(`nothing` = 5, `zero value` = 0))
#> Error in get_na_helper(x, as.tag): Package 'haven' required for this function. Please install it.
get_labels(x, values = "p")
#> Error: object 'x' not found
# replace old value labels
x <- add_labels(
efc$e42dep,
labels = c(`not so dependent` = 4, `lorem ipsum` = 5)
)
#> Error in get_na_helper(x, as.tag): Package 'haven' required for this function. Please install it.
get_labels(x, values = "p")
#> Error: object 'x' not found
# replace specific missing value (tagged NA)
if (require("haven")) {
x <- labelled(c(1:3, tagged_na("a", "c", "z"), 4:1),
c("Agreement" = 1, "Disagreement" = 4, "First" = tagged_na("c"),
"Refused" = tagged_na("a"), "Not home" = tagged_na("z")))
# get current NA values
x
# tagged NA(c) has currently the value label "First", will be
# replaced by "Second" now.
replace_labels(x, labels = c("Second" = tagged_na("c")))
}
#> Loading required package: haven
#> Warning: there is no package called ‘haven’
# remove_labels()
x <- remove_labels(efc$e42dep, labels = 2)
#> Error in get_na_helper(x, as.tag): Package 'haven' required for this function. Please install it.
get_labels(x, values = "p")
#> Error: object 'x' not found
x <- remove_labels(efc$e42dep, labels = "independent")
#> Error in get_na_helper(x, as.tag): Package 'haven' required for this function. Please install it.
get_labels(x, values = "p")
#> Error: object 'x' not found
if (require("haven")) {
x <- labelled(c(1:3, tagged_na("a", "c", "z"), 4:1),
c("Agreement" = 1, "Disagreement" = 4, "First" = tagged_na("c"),
"Refused" = tagged_na("a"), "Not home" = tagged_na("z")))
# get current NA values
get_na(x)
get_na(remove_labels(x, labels = tagged_na("c")))
}
#> Loading required package: haven
#> Warning: there is no package called ‘haven’