Reading SPSS file in R

I am trying to learn R and want to add an SPSS file that I can open in SPSS.

I 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 error received:

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

In addition, I tried to save the SPSS file as an SPSS 7..av file (previously used SPSS 18).

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

+49
r spss
Jun 28 '10 at 21:30
source share
13 answers

I had a similar problem and it was resolved after the read.spss prompt. Instead, instead of the memisc package memisc you can import a portable SPSS file:

 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 be missing some string values, while portable imports work without problems. The help page for spss.portable.file states:

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

+43
Sep 14 2018-12-12T00:
source share

read.spss seems a bit dated, so I used a package called memisc .

To make this work, follow these steps:

 install.packages("memisc") data <- as.data.set(spss.system.file('yourfile.sav')) 
+16
Sep 29 '13 at 7:11
source share

I know this post is old, but I also had problems loading the SPSS Qualtrics file into R R. The read.spss code came from PSPP a long time ago and did not update after some time. (And the Hmisc code also uses read.spss (), so you're out of luck.)

The good news is that PSPP 0.6.1 should read the files in perfect order while you specify “Line Width” in “Short-255 (SPSS 12.0 and earlier)” on the “Load Data” page in Qualtrics. Read it on PSPP, save a new copy, and you should be in business. Awkward but free.

alt text ,

+9
Dec 11 2018-10-12T00:
source share

It seems that the implementation of R read.spss is incomplete or broken. However, R2.10.1 is better than R2.8.1. It looks like R is frustrated by user attributes in sav file even from 2.10.1 (the last thing I have). R may also not understand the character encoding field in the file, and in particular, it probably does not work with Unicode SPSS files.

You can try to open the file in SPSS, delete any custom attributes and save the file. You can find out if there are custom attributes with the SPSS command

display attributes.

If so, delete them (see VARIABLE ATTRIBUTE and DATAFILE ATTRIBUTE commands) and try again.

NTN, John Peck

+5
Jun 28 '10 at 23:02
source share

You can read the SPSS file from R using the above solutions or the one you are currently using. Just make sure that the command is supplied with the file so that it can read correctly. I had the same error and the problem was that SPSS was not able to access this file. You must make sure that the path to the file is correct, the file is accessible and in the correct format.

 library(foreign) asq <- read.spss('ASQ2010.sav', to.data.frame=TRUE) 

As for the warning message , it does not affect the data. Record type 7 is used to store functions in the new SPSS software so that older SPSS software can read new data. But does not affect the data. I have used this many times and no data is lost.

You can also read about it at http://r.789695.n4.nabble.com/read-spss-warning-message-Unrecognized-record-type-7-subtype-18-encountered-in-system-file-td3000775 .html # a3007945

+4
Sep 21 '14 at 20:06
source share

You can also try the following:

 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) 
+4
Jan 19 '16 at 13:12
source share

If you have access to SPSS, save the file as .csv, so import it using read.csv or read.table . I can not remember the problems with importing the .sav file. So far, it has worked like a charm with read.spss and spss.get . I believe that spss.get will not give different results, since it depends on foreign::read.spss

Can you provide some information about the SPSS / R / Hmisc / foreign version?

+2
Jun 28 '10 at 23:38
source share

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

See an example here . However, I must admit that there may be problems with very large data files.

+2
Mar 03 '13 at 8:42
source share

For me, this works well with memisc!

 install.packages("memisc") load('memisc') Daten.Februar <-as.data.set(spss.system.file("NPS_Februar_15_Daten.sav")) names(Daten.Februar) 
+2
Sep 22 '15 at 10:10
source share

There is no such problem with the packages you use. The only requirement to read the spss file is to put the file in a PORTABLE file. I mean, the spss file has the * .sav extension. You need to convert your spss file to a portable document that uses the * .por extension.

At http://www.statmethods.net/input/importingdata.html

+1
Dec 28 2018-11-12T00:
source share

In my case, this warning was combined with the appearance of a new variable in front of the first column of my data with values ​​-100, 2, 2, 2, ..., a shift in the correspondence between labels and values, and removal of the last variable. The solution that worked (using SPSS) was to create a new dump variable in the last column of the file, fill it with random values ​​and execute the following code: (filename is the path to the sav file, and in my case the original SPSS file had 62 columns, therefore 63 with an additional mute variable)

 library(memisc) data <- as.data.set(spss.system.file(filename)) copyofdata = data for(i in 2:63){ names(data)[i] <- names(copyofdata)[i-1] } data[[1]] <- NULL newcopyofdata = data for(i in 2:62){ labels(data[[i]]) <- labels(newcopyofdata[[i-1]]) } labels(data[[1]]) <- NULL 

Hope the above code helps someone else.

+1
Jan 19 '15 at 6:58
source share

one)

I found a program, stat-transfer, useful for importing spss and stata files into R.

It solves the problem you specified by converting spss to a dataset R. It is also very useful for subsetting super large datasets into smaller pieces that can be used by R. Not free, but a very useful tool for working with datasets from different programs especially if you don’t have access to them.

2)

The Memisc package also has a spss function that is worth a try.

0
Jun 29 '10 at 0:01
source share

Disable your UNICODE in SPSS

Open SPSS without opening any data and run the code below in the syntax editor

 SET UNICODE OFF. 

Open the data set and save it to remove Unicode

read.spss('yourdata.sav', to.data.frame=T) works correctly, then

0
Aug 15 '16 at 10:59
source share



All Articles