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
R can import datasets from SPSS with the function read.
The easiest way to import SPSS files into R is to use the read_sav() function from the haven library.
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.
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).
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.
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'))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With