Learn R Programming

sjmisc (version 1.0.3)

get_val_labels: Retrieve value labels of a data frame or variable

Description

This function retrieves the value labels of an imported SPSS, SAS or STATA data set (via read_spss, read_sas or read_stata) and
  • ifxis a data frame or list of variables, returns the all variable's value labels aslist
  • or, ifxis a vector, returns the label as string.

Usage

get_val_labels(x, include.values = FALSE)

Arguments

x
a data.frame with variables that have attached value labels (e.g. from an imported SPSS, SAS or STATA data set, via read_spss, read_sas or
include.values
logical, if TRUE, the values associated with the value labels are returned as well (as names attribute of the returned object).

Value

  • Either a list with all value labels from all variables if x is a data.frame or list; a string with the value labels, if x is a variable; or NULL if no value label attribute was found.

Details

This package can add (and read) value and variable labels either in foreign package style (attributes are named value.labels and variable.label) or in haven package style (attributes are named labels and label). By default, the haven package style is used. Working with labelled data is a key element of the sjPlot package, which accesses these attributes to automatically read label attributes for labelling axis categories and titles or table rows and columns. When working with labelled data, you can, e.g., use get_var_labels or get_val_labels to get a vector of value and variable labels, which can then be used with other functions like barplot etc. See 'Examples'. 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 (i.e. the names of the label attributes, see above) via options(value_labels = "haven") or options(value_labels = "foreign").

See Also

The sjPlot manual on http://www.strengejacke.de/sjPlot/datainit/{data initialization}, http://www.strengejacke.de/sjPlot/labelleddata/{working with labelled data} or http://www.strengejacke.de/sjPlot/view_spss/{inspecting (SPSS imported) data frames} for more details; set_val_labels to manually set value labels, get_var_labels to get variable labels and get_values to retrieve value label associated values.

Examples

Run this code
# import SPSS data set
# mydat <- read_spss("my_spss_data.sav", enc="UTF-8")

# retrieve variable labels
# mydat.var <- get_var_labels(mydat)

# retrieve value labels
# mydat.val <- get_val_labels(mydat)

data(efc)
get_val_labels(efc$e42dep)

# simple barplot
barplot(table(efc$e42dep))
# get value labels to annotate barplot
barplot(table(efc$e42dep),
        names.arg = get_val_labels(efc$e42dep),
        main = get_var_labels(efc$e42dep))

# include associated values
get_val_labels(efc$e42dep, TRUE)

# get labels from multiple variables
get_val_labels(list(efc$e42dep,
                    efc$e16sex,
                    efc$e15relat))

Run the code above in your browser using DataLab