Learn R Programming

sjlabelled (version 1.2.0)

read_spss: Import data from other statistical software packages

Description

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

Usage

read_spss(
  path,
  convert.factors = TRUE,
  drop.labels = FALSE,
  tag.na = FALSE,
  encoding = NULL,
  verbose = FALSE,
  atomic.to.fac = convert.factors
)

read_sas( path, path.cat = NULL, convert.factors = TRUE, drop.labels = FALSE, encoding = NULL, verbose = FALSE, atomic.to.fac = convert.factors )

read_stata( path, convert.factors = TRUE, drop.labels = FALSE, encoding = NULL, verbose = FALSE, atomic.to.fac = convert.factors )

read_data( path, convert.factors = TRUE, drop.labels = FALSE, encoding = NULL, verbose = FALSE, atomic.to.fac = convert.factors )

Arguments

path

File path to the data file.

convert.factors

Logical, if TRUE, categorical variables imported from the dataset (which are imported as atomic) will be converted to factors. Variables are considered as categorical if they have at least the same number of value labels as unique values. This prevents that ranges of continuous variables, where - for instance - the minimum and maximum values are labelled only, will also be converted to factors.

drop.labels

Logical, if TRUE, unused value labels are removed. See drop_labels.

tag.na

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

encoding

The character encoding used for the file. This defaults to the encoding specified in the file, or UTF-8. Use this argument to override the default encoding stored in the file.

verbose

Logical, if TRUE, a progress bar is displayed that indicates the progress of converting the imported data.

atomic.to.fac

Deprecated, please use `convert.factors` instead.

path.cat

Optional, the file path to the SAS catalog file.

Value

A data frame containing the imported, labelled data. Retrieve value labels with get_labels and variable labels with get_label.

Details

These read-functions behave slightly differently from haven's read-functions:

  • The vectors in the returned data frame are of class atomic, not of class labelled. The labelled-class might cause issues with other packages.

  • When importing SPSS data, variables with user defined missings won't be read into labelled_spss objects, but imported as tagged NA values.

The convert.factors 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.

See Also

Vignette Labelled Data and the sjlabelled-Package.

Examples

Run this code
# 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", convert.factors = TRUE)

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

# retrieve value labels
mydat.val <- get_labels(mydat)
# }

Run the code above in your browser using DataLab