Learn R Programming

sjmisc (version 1.2)

set_labels: Add value labels to variables

Description

This function adds character labels as attribute (named "labels" or "value.labels") to a variable or vector x, resp. to a set of variables in a data.frame or a list-object. These value labels will be accessed by functions of the sjPlot package, in order to automatically set values or legend labels, however, sjmisc provides functions to quickly access these attributes for other purposes.

Usage

set_labels(x, labels, force.labels = FALSE, force.values = TRUE)

set_val_labels(x, labels, force.labels = FALSE, force.values = TRUE)

Arguments

x
Variable (vector), list of variables or a data.frame where value label attributes should be added. Replaces former value labels.
labels
(Named) character vector of labels that will be added to x as "labels" or "value.labels" attribute.
  • iflabelsisnotanamed vector, its length must equal the value range of<
force.labels
Logical; if TRUE, all labels are added as value label attribute, even if x has less unique values then length of labels or if x has a smaller range then length of labels. See 'E
force.values
Logical, if TRUE (default) and labels has less elements than unique values of x, additional values not covered by labels will be added as label as well. See 'Examples'. This parameter will be ignored, if

Value

  • x with value label attributes; or with removed label-attributes if labels = "".

Details

See 'Details' in get_labels.

See Also

See package vignettes or http://www.strengejacke.de/sjPlot/{online documentation} for more details; set_label to manually set variable labels or get_label to get variable labels; add_labels to add additional value labels without replacing the existing ones.

Examples

Run this code
library(sjPlot)
dummy <- sample(1:4, 40, replace = TRUE)
sjp.frq(dummy)

dummy <- set_labels(dummy, c("very low", "low", "mid", "hi"))
sjp.frq(dummy)

# force using all labels, even if not all labels
# have associated values in vector
x <- c(2, 2, 3, 3, 2)
# only two value labels
x <- set_labels(x, c("1", "2", "3"))
x
sjp.frq(x)
# all three value labels
x <- set_labels(x, c("1", "2", "3"), force.labels = TRUE)
x
sjp.frq(x)

# create vector
x <- c(1, 2, 3, 2, 4, NA)
# add less labels than values
x <- set_labels(x, c("yes", "maybe", "no"), force.values = FALSE)
x
# add all necessary labels
x <- set_labels(x, c("yes", "maybe", "no"), force.values = TRUE)
x

# set labels and missings
x <- c(1, 1, 1, 2, 2, -2, 3, 3, 3, 3, 3, 9)
x <- set_labels(x, c("Refused", "One", "Two", "Three", "Missing"))
x

x <- set_na(x, c(-2, 9), as.attr = TRUE)
x
summary(as_labelled(x))


# set labels via named vector,
# not using all possible values
data(efc)
get_labels(efc$e42dep)

x <- set_labels(efc$e42dep, c(`1` = "independent",
                              `4` = "severe dependency",
                              `9` = "missing value"))
get_labels(x, attr.only = TRUE, include.values = "p")


# setting same value labels to multiple vectors
# create a set of dummy variables
dummy1 <- sample(1:4, 40, replace = TRUE)
dummy2 <- sample(1:4, 40, replace = TRUE)
dummy3 <- sample(1:4, 40, replace = TRUE)
# put them in list-object
dummies <- list(dummy1, dummy2, dummy3)
# and set same value labels for all three dummies
dummies <- set_labels(dummies, c("very low", "low", "mid", "hi"))
# see result...
get_labels(dummies)

Run the code above in your browser using DataLab