Learn R Programming

sjmisc (version 1.8)

read_spss: Import SPSS dataset as data frame into R

Description

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

Usage

read_spss(path, enc = NA, attach.var.labels = FALSE, atomic.to.fac = FALSE, keep.na = FALSE, option = "haven")

Arguments

path
File path to the data file.
enc
File encoding of the data file. See 'Details'.
attach.var.labels
Logical, if TRUE, variable labels will automatically be added to each variable as "variable.label" attribute. Use this parameter, if option = "foreign", where variable labels are added as list-attribute to the imported data frame. Not needed if option = "haven" (default).
atomic.to.fac
Logical, if TRUE, factor variables imported from the dataset (which are imported as atomic) will be converted to factors.
keep.na
Logical, if TRUE, user-defined missing values will be left as their original codes. If FALSE (default), corresponding values are converted to NA.
option
String, indicating which package will be used to read the SPSS data file. By default, option = "haven", which means, the read_spss function from the haven package is used. Use option = "foreign" to use foreign's read.spss function. Use options(read_spss = "foreign") to make this function always use the foreign package read.spss function.

Value

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

Details

In some cases, column names of the imported data set are not properly encoded. Use the enc-argument to specify the character encoding for the SPSS data set (like enc = "UTF-8", see Encoding).

See Also

Examples

Run this code
## Not run: 
# # import SPSS data set. uses haven's read function
# # by default
# mydat <- read_spss("my_spss_data.sav")
# 
# # use foreign's read function
# mydat <- read_spss("my_spss_data.sav",
#                    enc = "UTF-8",
#                    option = "foreign")
# 
# # 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)

Run the code above in your browser using DataLab