Import SPSS dataset as data frame into R

Share:

Description

Import data from SPSS, including NA's, value and variable labels.

Usage

1
read_spss(path, atomic.to.fac = FALSE, tag.na = FALSE)

Arguments

path

File path to the data file.

atomic.to.fac

Logical, if TRUE, categorical variables imported from the dataset (which are imported as atomic) will be converted to factors.

tag.na

Logical, if TRUE, missing values are imported as tagged_na values; else, missing values are converted to regular NA (default behaviour).

Details

The atomic.to.fac option only converts those variables into factors that are of class atomic and which have value labels after import. Atomic vectors without value labels are considered as continuous and not converted to factors.

Value

A data frame containing the SPSS data. Retrieve value labels with get_labels and variable labels with get_label.

Note

This is a wrapper function for read_spss of the haven package. This function adds value and variable labels as attributes to the imported variables of the data frame.

Most functions of the sjPlot package access value and variable label attributes to automatically detect labels in order to set them as axis, legend or title labels in plots (sjp.-functions) respectively as column or row headers in table outputs (sjt.-functions). See online manual for more details.

When working with labelled data, you can, e.g., use get_label or get_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_labels.

See Also

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
## Not run: 
# import SPSS data set. uses haven's read function
mydat <- read_spss("my_spss_data.sav")

# use haven's read function, convert atomic to factor
mydat <- read_spss("my_spss_data.sav", atomic.to.fac = TRUE)

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

# retrieve value labels
mydat.val <- get_labels(mydat)
## End(Not run)

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.