Learn R Programming

sjmisc (version 1.8)

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_labels(x, force.labels = FALSE, force.values = TRUE) <- value

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.
  • if labels is not a named vector, its length must equal the value range of x, i.e. if x has values from 1 to 3, labels should have a length of 3;
  • if length of labels is intended to differ from length of unique values of x, a warning is given. You can still add missing labels with the force.labels or force.values arguments; see 'Note'.
  • if labels is a named vector, value labels will be set accordingly, even if x has a different length of unique values. See 'Note' and 'Examples'.
  • if x is a data frame, labels may also be a list of (named) character vectors;
  • if labels is a list, it must have the same length as number of columns of x;
  • if labels is a vector and x is a data frame, labels will be applied to each column of x.

Use labels = "" to remove labels-attribute from x.

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 'Examples'. This parameter will be ignored, if labels is a named vector.
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 labels is a named vector.
value
See labels,

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 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
## Not run: 
# 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)## End(Not run)

# 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

# or use:
# set_labels(x) <- c("1", "2", "3")

## Not run: 
# sjp.frq(x)## End(Not run)
# all three value labels
x <- set_labels(x, c("1", "2", "3"), force.labels = TRUE)
x
## Not run: 
# sjp.frq(x)## End(Not run)

# 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
frq(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(`independent` = 1,
                             `severe dependency` = 2,
                             `missing value` = 9))
get_labels(x, include.values = "p")

get_labels(x, include.values = "p", include.non.labelled = TRUE)


# 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