Learn R Programming

sjmisc (version 1.0.0)

set_var_labels: Attach variable label(s) to a single variable or data frame

Description

This function sets variable labels to a single variable or to a set of variables in a data frame. To each variable, the attribute "label" or "variable.label" with the related variable name is attached. Most of this package's functions can automatically retrieve the variable name to use it as axis labels or plot title (see details).

Usage

set_var_labels(x, lab, attr.string = NULL)

Arguments

x
A single variable (vector) or data frame with variables.
lab
If x is a vector (single variable), use a single character string with the variable label for x. If x is a data.frame, use a vector with character labels of same le
attr.string
The attribute string for the variable label. To ensure compatibility to the foreign-package, use the default string "variable.label". If you want to save data with the haven package, use attr.string = "label"

Value

  • x, with attached variable label attribute(s), which contains the variable name(s).

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_var_labels

Examples

Run this code
# sample data set, imported from SPSS. Variable labels are attached
# as attribute to the data frame (so variables currently don't have this attribute)
data(efc)
# get variable labels
variable.labels <- get_var_labels(efc)
# set variable labels as attribute to each single variable of data frame
efc <- set_var_labels(efc, variable.labels)

sjt.frq(efc$e42dep)
sjt.frq(data.frame(efc$e42dep, efc$e16sex))

# ---------------------------------------------
# manually set value and variable labels
# ---------------------------------------------
dummy <- sample(1:4, 40, replace=TRUE)
dummy <- set_val_labels(dummy, c("very low", "low", "mid", "hi"))
dummy <- set_var_labels(dummy, "Dummy-variable")
# auto-detection of value labels by default, auto-detection of
# variable labels if parameter "title" set to NULL.
sjp.frq(dummy, title = NULL)

Run the code above in your browser using DataLab