Learn R Programming

sjmisc (version 1.0.0)

get_var_labels: Retrieve variable labels of (an SPSS-imported) data frame or of a specific variable

Description

This function retrieves the variable labels of an imported SPSS data set (via read_spss) and
  • ifxis a data frame, returns the all variable labels aslistobject
  • or, ifxis a vector, returns the variable label as string.

Usage

get_var_labels(x)

Arguments

x
A data frame (containing imported SPSS data or with attached variable labels) or a vector with "label" or "variable.label" attribute.

Value

  • A named char vector with all variable labels from the SPSS dataset, or a simple string vector with the variable label, if x is a variable.

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}
  • set_var_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)
# sample data set has not attached variable labels to each vector
# so we have to do this first... use 'autoAttachVarLabels' in
# function 'read_spss' to automatically perform this step.
efc <- set_var_labels(efc, get_var_labels(efc))

# get variable lable
get_var_labels(efc$e42dep)

# alternative way
get_var_labels(efc)["e42dep"]

Run the code above in your browser using DataLab