Read SPSS file into R

后端 未结 14 1733
情歌与酒
情歌与酒 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 15:07

    In my case this warning was combined with a appearance of a new variable before first column of my data with values -100, 2, 2, 2, ..., a shift in the correspondence between labels and values and the deletion of the last variable. A solution that worked was (using SPSS) 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, thus 63 with the additional dumb 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 will help someone else.

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

    The read.spss seems to be outdated a little bit, so I used package called memisc.

    To get this to work do this:

    install.packages("memisc")
    data <- as.data.set(spss.system.file('yourfile.sav'))
    
    0 讨论(0)
提交回复
热议问题