Converting an SPSS sav file to an R workspace

From InterSciWiki

Jump to: navigation, search


Multilevel Modeling in R see page 15

2.4.3 The foreign package and SPSS files

Included in current versions of R is the “foreign” package. This package contains functions to import SPSS, SAS, Stata and minitab files.

> library(foreign)

> search() [1] ".GlobalEnv" "package:foreign" "package:multilevel" [4] "package:methods" "package:stats" "package:graphics" [7] "package:grDevices" "package:utils" "package:datasets" [10] "Autoloads" "package:base" > objects(2) [1] "data.restore" "lookup.xport" "read.dbf" "read.dta" [5] "read.epiinfo" "read.mtp" "read.octave" "read.S" [9] "read.spss" "read.ssd" "read.systat" "read.xport" [13] "write.dbf" "write.dta" "write.foreign" For example, if the data in cohesion is stored in an SPSS sav file in a TEMP directory, then one could issue the following command to read in the data (text following the # mark is a comment): > help(read.spss) #look at the documentation on read.spss > cohesion2<-read.spss("c:\\temp\\cohesion.sav") > cohesion2 #look at the cohesion object $UNIT [1] "1044B" "1044B" "1044B" "1044B" "1044B" "1044B" "1044C" "1044C" "1044C" [10] "1044C" "1044C" $PLATOON [1] "1ST" "1ST" "1ST" "2ND" "2ND" "2ND" "1ST" "1ST" "2ND" "2ND" "2ND" $COH01 [1] 4 3 2 3 4 3 3 3 3 2 1 $COH02 [1] 5 NA 3 4 4 3 3 1 3 2 1 $COH03 [1] 5 5 3 3 3 2 3 4 3 2 1 $COH04 [1] 5 5 3 4 4 2 3 3 3 3 3 $COH05 [1] 5 5 3 4 4 1 3 4 3 2 3 attr(,"label.table") attr(,"label.table")$UNIT NULL attr(,"label.table")$PLATOON NULL attr(,"label.table")$COH01 NULL attr(,"label.table")$COH02 NULL attr(,"label.table")$COH03 NULL attr(,"label.table")$COH04 NULL attr(,"label.table")$COH05 NULL The cohesion2 object is stored as a list rather than a dataframe. With the default options, read.spss function imports the file as a list and reads information about data labels. In almost every case, users will want to convert the list object into a dataframe for manipulation in R. This can be done using the data.frame command. > cohesion2<-data.frame(cohesion2) > cohesion2

Page 17 Multilevel Models in R 17 UNIT PLATOON COH01 COH02 COH03 COH04 COH05 1 1044B 1ST 4 5 5 5 5 2 1044B 1ST 3 NA 5 5 5 3 1044B 1ST 2 3 3 3 3 4 1044B 2ND 3 4 3 4 4 5 1044B 2ND 4 4 3 4 4 6 1044B 2ND 3 3 2 2 1 7 1044C 1ST 3 3 3 3 3 8 1044C 1ST 3 1 4 3 4 9 1044C 2ND 3 3 3 3 3 10 1044C 2ND 2 2 2 3 2 11 1044C 2ND 1 1 1 3 3 Alternatively, users can change the default options in read.spss to read the data directly into a dataframe. Note the use of use.value.labels=F and to.data.frame=T below: > cohesion2<-read.spss("c:\\temp\\cohesion.sav", use.value.labels=F, to.data.frame=T) > cohesion2 UNIT PLATOON COH01 COH02 COH03 COH04 COH05 1 1044B 1ST 4 5 5 5 5 2 1044B 1ST 3 NA 5 5 5 3 1044B 1ST 2 3 3 3 3 4 1044B 2ND 3 4 3 4 4 5 1044B 2ND 4 4 3 4 4 6 1044B 2ND 3 3 2 2 1 7 1044C 1ST 3 3 3 3 3 8 1044C 1ST 3 1 4 3 4 9 1044C 2ND 3 3 3 3 3 10 1044C 2ND 2 2 2 3 2 11 1044C 2ND 1 1 1 3 3 The cohesion dataframe (made using the EXCEL and csv files) and cohesion2 (imported from SPSS) are now identical.

Personal tools