Learn R Programming

sjmisc (version 1.0.2)

get_val_labels: Retrieve value labels of a variable or an imported data frame

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, returns the all variable's value labels aslistobject
  • or, ifxis a vector, returns the label as string.

Usage

get_val_labels(x)

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

Value

  • Either a list with all value labels from the data frame's variables, 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 (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. When working with other packages, 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' from get_val_labels. 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

  • http://www.strengejacke.de/sjPlot/datainit/{sjPlot manual: data initialization}
  • http://www.strengejacke.de/sjPlot/view_spss/{sjPlot manual: inspecting (SPSS imported) data frames}
  • set_val_labels

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))

Run the code above in your browser using DataLab