简体   繁体   中英

reading SPSS file into R throwing error - error reading system-file header

Lately, I sometimes get an error when reading SPSS files using read.spss from the foreign package:

Error in read.spss("sample.sav") : error reading system-file header In addition: Warning message: In read.spss("sample.sav") : sample.sav: Bad format specifier byte (0)

I produced a tiny sample.sav file with just one variable and 3 cases that will cause the error. Download the file or use

download.file("http://134.102.100.220/~mark/sample.sav", "sample.sav")
read.spss("sample.sav")

Any ideas?

My system

R version 3.1.1 (2014-07-10)
Platform: x86_64-apple-darwin10.8.0 (64-bit)
foreign: Version 0.8-63

locale:
[1] en_US.UTF-8/de_DE.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

$FL2@(#) IBM SPSS STATISTICS DATA FILE 64-bit Macintosh 20.0.0 Y@24 Mar 1515:00:55electric paper VAR1 None – È˝ ˇˇˇˇˇˇÔˇˇˇˇˇˇˇÔ˛ˇˇˇˇˇÔˇ

That is the header viewed in a simple text editor (TextEdit.app). So reading the help file for read.spss, one sees that it suggested using:

install.packages("memisc")

?memisc::spss.system.file
 memisc::spss.system.file("~/Downloads/sample.sav")
#-=----------------
SPSS system file '/Users/davidwinsemius/Downloads/sample.sav' 
    with 1 variables and 3 observations
 inp <- memisc::spss.system.file("~/Downloads/sample.sav")

 actual <- memisc::subset(inp, select= c(var1=var1))
 actual

Data set with 3 observations and 1 variables

  var1
1    1
2    2
3    3

The moral of the story: Sometimes is is better to read all of the help file. Since I have in the past read that same help page, I was surprised to find that it had been modified. In the past there comments regarding version limitations which now seem to have been removed.

I would use the haven package, rather than foreign , to read spss files:

require("haven")
sample <- read_spss("sample.sav")
View(sample)

You could alternatively use the sjPlot package, which uses haven to do its heavy lifting:

require("sjPlot")
sample <- sjPlot::read_spss("sample.sav", option = "haven")
View(sample)

Using sjPlot , you can also view the variable labels and values:

sjPlot::view_spss(sample)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM