Learn R Programming

sjmisc (version 1.0.3)

set_val_labels: Attach value labels to variables

Description

This function adds character labels as attribute to a variable or vector x, resp. to a set of variables in a data.frame or list-object. To each variable, the attribute "labels" or "value.labels" with the related labels is attached. These value labels will be accessed by functions of the sjPlot package, in order to automatically set values or legend labels.

Usage

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

Arguments

x
a variable (vector), list of variables or a data.frame where value labels should be attached. Replaces former value labels.
labels
a character vector of labels that will be attached to x by setting the "labels" or "value.labels" attribute. The length of this character vector must equal the value range of x, i.e. if x ha
force.labels
logical; if TRUE, all labels are added as value labels attribute, even if x has less unique values then length of labels or if x has a smaller range then length of labels. See '

Value

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

Details

See 'Details' in get_val_labels

See Also

The sjPlot manual on http://www.strengejacke.de/sjPlot/datainit/{data initialization} or http://www.strengejacke.de/sjPlot/view_spss/{inspecting (SPSS imported) data frames} for more details; set_var_labels to manually set variable labels or get_var_labels to get variable labels.

Examples

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

dummy <- set_val_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_val_labels(x, c("1", "2", "3"))
x
sjp.frq(x)
# all three value labels
x <- set_val_labels(x, c("1", "2", "3"), force.labels = TRUE)
x
sjp.frq(x)

# 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_val_labels(dummies, c("very low", "low", "mid", "hi"))
# see result...
get_val_labels(dummies)

Run the code above in your browser using DataLab