Learn R Programming

sjmisc (version 1.0.0)

set_val_labels: Attach value labels to a variable or vector

Description

This function attaches character labels as "value.labels" attribute to a variable or vector "x", resp. to all variables of a data frame if "x" is a data.frame. These value labels will be accessed by most of this package's functions, in order to automatically set values or legend labels.

Usage

set_val_labels(x, labels)

Arguments

x
a variable (vector) or a data frame where 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

Value

  • x with attached value labels.

Details

This package can add (and read) value and variable labels either in foreign package style (value.labels and variable.label) or in haven package style (labels and label). By default, the haven package style is used. The sjPlot package accesses these attributes to automatically read label attributes for labelling axes categories and titles or table rows and columns. Furthermore, value and variable labels are used when saving data, e.g. to SPSS (see write_spss), which means that the written SPSS file contains proper labels for each variable. You can set a default label style via options(value_labels = "haven") or options(value_labels = "foreign").

See Also

  • http://www.strengejacke.de/sjPlot/datainit/{sjPlot manual: data initialization}
  • http://www.strengejacke.de/sjPlot/view_spss/{sjPlot manual: inspecting (SPSS imported) data frames}
  • get_val_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)

Run the code above in your browser using DataLab