R/add_labels.R, R/remove_labels.R
add_labels.RdThese functions add, replace or remove value labels to or from variables.
add_labels(x, ..., labels)
replace_labels(x, ..., labels)
remove_labels(x, ..., labels)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'.
add_labels()A named (numeric) vector of labels
that will be added to x as label attribute.
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.
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.
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().
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)
#> [1] "independent" "slightly dependent" "moderately dependent"
#> [4] "severely dependent"
x <- add_labels(efc$e42dep, labels = c(`nothing` = 5))
get_labels(x)
#> [1] "independent" "slightly dependent" "moderately dependent"
#> [4] "severely dependent" "nothing"
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)
}
#> $e42dep
#> [1] "independent" "slightly dependent" "moderately dependent"
#> [4] "severely dependent" "nothing"
#>
#> $c172code
#> [1] "low level of education" "intermediate level of education"
#> [3] "high level of education" "nothing"
#>
#> $c161sex
#> [1] "Male" "Female"
#>
x <- add_labels(efc$e42dep, labels = c(`nothing` = 5, `zero value` = 0))
get_labels(x, values = "p")
#> [1] "[0] zero value" "[1] independent"
#> [3] "[2] slightly dependent" "[3] moderately dependent"
#> [5] "[4] severely dependent" "[5] nothing"
# replace old value labels
x <- add_labels(
efc$e42dep,
labels = c(`not so dependent` = 4, `lorem ipsum` = 5)
)
#> label 'severely dependent' was replaced with new value label.
get_labels(x, values = "p")
#> [1] "[1] independent" "[2] slightly dependent"
#> [3] "[3] moderately dependent" "[4] not so dependent"
#> [5] "[5] lorem ipsum"
# 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")))
}
#> tagged NA 'First' was replaced with new value label.
#> <labelled<double>[10]>
#> [1] 1 2 3 NA(a) NA(c) NA(z) 4 3 2 1
#>
#> Labels:
#> value label
#> 1 Agreement
#> 4 Disagreement
#> NA(c) Second
#> NA(a) Refused
#> NA(z) Not home
# remove_labels()
x <- remove_labels(efc$e42dep, labels = 2)
get_labels(x, values = "p")
#> [1] "[1] independent" "[3] moderately dependent"
#> [3] "[4] severely dependent"
x <- remove_labels(efc$e42dep, labels = "independent")
get_labels(x, values = "p")
#> [1] "[2] slightly dependent" "[3] moderately dependent"
#> [3] "[4] severely dependent"
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")))
}
#> Refused Not home
#> NA NA