Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read SPSS file into R

Tags:

r

spss

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

like image 661
Btibert3 Avatar asked Jun 28 '10 21:06

Btibert3


People also ask

Can you read SPSS file in R?

R can import datasets from SPSS with the function read.

How do I read SPSS in R studio?

The easiest way to import SPSS files into R is to use the read_sav() function from the haven library.

How do I read a .SAV file?

To open the SAV file using ClientSettings Editor, simply select File → Open, navigate to your SAV file, and click Open. When you are finished modifying the file, select File → Save or Save As, name the file, choose the save location, and click Save.

What package is read SPSS in?

read. spss reads a file stored by the SPSS save or export commands. This was orignally written in 2000 and has limited support for changes in SPSS formats since (which have not been many).


2 Answers

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.

like image 158
ggll Avatar answered Oct 17 '22 23:10

ggll


The read.spss seems to be outdated a little bit, so I used package called memisc.

To get this to work do this:

install.packages("memisc") data <- as.data.set(spss.system.file('yourfile.sav')) 
like image 25
Jaanus Avatar answered Oct 18 '22 00:10

Jaanus