Read SPSS file into R

后端 未结 14 1732
情歌与酒
情歌与酒 2020-12-12 14:17

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 s

相关标签:
14条回答
  • 2020-12-12 14:58

    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?

    0 讨论(0)
  • 2020-12-12 14:58

    I agree with @SDahm that the haven package would be the way to go. I myself have struggled a bit with string values when starting to use it, so I thought I'd share my approach on that here, too.

    The "semantics" vignette has some useful information on this topic.

    library(tidyverse)
    library(haven)
    
    # Some interesting information in here
    vignette('semantics')
    
    # Get data from spss file
    df <- read_sav(path_to_file)
    
    # get value labels
    df <- map_df(.x = df, .f = function(x) {
      if (class(x) == 'labelled') as_factor(x)
      else x})
    # get column names
    colnames(df) <- map(.x = spss_file, .f = function(x) {attr(x, 'label')})
    
    0 讨论(0)
  • 2020-12-12 14:59

    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.

    0 讨论(0)
  • 2020-12-12 15:04

    You can read SPSS file from R using above solutions or the one you are currently using. Just make sure that the command is fed with the file, that it can read properly. I had same error and the problem was, SPSS could not access that file. You should make sure the file path is correct, file is accessible and it is in correct format.

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

    As far as warning message is concerned, It does not affect the data. The record type 7 is used to store features in newer SPSS software to make older SPSS software able to read new data. But does not affect data. I have used this numerous times and data is not lost.

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

    0 讨论(0)
  • 2020-12-12 15:07

    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)
    
    0 讨论(0)
  • 2020-12-12 15:07

    It looks like the R read.spss implementation is incomplete or broken. R2.10.1 does better than R2.8.1, however. It appears that R gets upset about custom attributes in a sav file even with 2.10.1 (The latest I have). R also may not understand the character encoding field in the file, and in particular it probably does not work with SPSS Unicode files.

    You might try opening the file in SPSS, deleting any custom attributes, and resaving the file. You can see whether there are custom attributes with the SPSS command

    display attributes.

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

    HTH, Jon Peck

    0 讨论(0)
提交回复
热议问题