Read SPSS file into R

rspss

I am trying to learn R and want to bring in an SPSS file, which I can open in SPSS.

I have tried using read.spss from foreign and spss.get from Hmisc. Both error messages are the same.

Here is my code:

## install.packages("Hmisc")
library(foreign)

## change the working directory
getwd()
setwd('C:/Documents and Settings/BTIBERT/Desktop/')

## load in the file
## ?read.spss
asq <- read.spss('ASQ2010.sav', to.data.frame=T)

And the resulting error:

Error in read.spss("ASQ2010.sav", to.data.frame = T) : error
reading system-file header In addition: Warning message: In
read.spss("ASQ2010.sav", to.data.frame = T) : ASQ2010.sav: position
0: character `\000' (

Also, I tried saving out the SPSS file as a SPSS 7 .sav file (was previously using SPSS 18).

Warning messages: 1: In read.spss("ASQ2010_test.sav", to.data.frame =
T) : ASQ2010_test.sav: Unrecognized record type 7, subtype 14
encountered in system file 2: In read.spss("ASQ2010_test.sav",
to.data.frame = T) : ASQ2010_test.sav: Unrecognized record type 7,
subtype 18 encountered in system file

Best Answer

I had a similar issue and solved it following a hint in read.spss help. Using package memisc instead, you can import a portable SPSS file like this:

data <- as.data.set(spss.portable.file("filename.por"))

Similarly, for .sav files:

data <- as.data.set(spss.system.file('filename.sav'))

although in this case I seem to miss some string values, while the portable import works seamlessly. The help page for spss.portable.file claims:

The importer mechanism is more flexible and extensible than read.spss and read.dta of package "foreign", as most of the parsing of the file headers is done in R. They are also adapted to load efficiently large data sets. Most importantly, importer objects support the labels, missing.values, and descriptions, provided by this package.

Related Topic