| add_labels | R Documentation |
These functions add, replace or remove value labels to or from variables.
add_labels(x, ..., labels) replace_labels(x, ..., labels) remove_labels(x, ..., labels)
x |
A vector or data frame. |
... |
Optional, unquoted names of variables that should be selected for
further processing. Required, if |
labels |
|
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().
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.
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).
# add_labels()
data(efc)
get_labels(efc$e42dep)
x <- add_labels(efc$e42dep, labels = c(`nothing` = 5))
get_labels(x)
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)
}
x <- add_labels(efc$e42dep, labels = c(`nothing` = 5, `zero value` = 0))
get_labels(x, values = "p")
# replace old value labels
x <- add_labels(
efc$e42dep,
labels = c(`not so dependent` = 4, `lorem ipsum` = 5)
)
get_labels(x, values = "p")
# 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")))
}
# remove_labels()
x <- remove_labels(efc$e42dep, labels = 2)
get_labels(x, values = "p")
x <- remove_labels(efc$e42dep, labels = "independent")
get_labels(x, values = "p")
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")))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.