Read SPSS file into R

2019-01-21 02:44发布

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 spss
14条回答
何必那么认真
2楼-- · 2019-01-21 03:14

I know this post is old, but I also had problems loading a Qualtrics SPSS file into R. R's read.spss code came from PSPP a long time ago, and hasn't been updated in a while. (And Hmisc's code uses read.spss(), too, so no luck there.)

The good news is that PSPP 0.6.1 should read the files fine, as long as you specify a "String Width" of "Short - 255 (SPSS 12.0 and earlier)" on the "Download Data" page in Qualtrics. Read it into PSPP, save a new copy, and you should be in business. Awkward, but free.

alt text,

查看更多
Animai°情兽
3楼-- · 2019-01-21 03:14

For me it works well using memisc!

install.packages("memisc")
load('memisc')
Daten.Februar <-as.data.set(spss.system.file("NPS_Februar_15_Daten.sav"))
names(Daten.Februar)
查看更多
混吃等死
4楼-- · 2019-01-21 03:16

Another solution not mentioned here is to read SPSS data in R via ODBC. You need:

  1. IBM SPSS Statistics Data File Driver. Standalone driver is enough.
  2. Import SPSS data using RODBC package in R.

See the example here. However I have to admit that, there could be problems with very big data files.

查看更多
\"骚年 ilove
5楼-- · 2019-01-21 03:17

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.

查看更多
SAY GOODBYE
6楼-- · 2019-01-21 03:19

If you have access to SPSS, save file as .csv, hence import it with read.csv or read.table. I can't recall any problem with .sav file importing. So far it was working like a charm both with read.spss and spss.get. I reckon that spss.get will not give different results, since it depends on foreign::read.spss

Can you provide some info on SPSS/R/Hmisc/foreign version?

查看更多
【Aperson】
7楼-- · 2019-01-21 03:20

You may also try this:

setwd("C:/Users/rest of your path")

library(haven)
data <- read_sav("data.sav")

and if you want to read all files from one folder:

temp <- list.files(pattern = "*.sav")
read.all <- sapply(temp, read_sav)
查看更多
登录 后发表回答