Learn R Programming

sjmisc (version 2.0.1)

add_labels: Add or replace value labels of variables

Description

This function adds additional labels as attribute to a variable or vector x, resp. to a set of variables in a data.frame or list-object. Unlike set_labels, add_labels does not completely replace existing value labels (and hence, it does not remove non-specified labels), but adds value to the existing value labels of x. add_labels also replaces existing value labels, but unlike set_labels, preserves the remaining labels. See 'Note'.

Usage

add_labels(x, value)
add_labels(x) <- value
replace_labels(x, value)
replace_labels(x) <- value

Arguments

x
Variable (vector), list of variables or a data.frame where value label attributes should be added.
value
Named (numeric) vector of labels that will be added to x as label attribute. If x is a data frame, value may also be a list of named character vectors. If value is a list, it must have the same length as number of columns of x. If value is a vector and x is a data frame, value will be applied to each column of x.

Value

x with additional value 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

Run this code
data(efc)
get_labels(efc$e42dep)

x <- add_labels(efc$e42dep, c(`nothing` = 5))
get_labels(x)

x <- add_labels(efc$e42dep, c(`nothing` = 5, `zero value` = 0))
get_labels(x, include.values = "p")

# replace old values
x <- add_labels(efc$e42dep, c(`not so dependent` = 4, `lorem ipsum` = 5))
get_labels(x, include.values = "p")

# replace values, alternative function call
replace_labels(x) <- c(`new second` = 2)

# replace specific missing value (tagged NA)
library(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, c("Second" = tagged_na("c")))


Run the code above in your browser using DataLab