Skip to content

Instantly share code, notes, and snippets.

@malomarrec
Created June 2, 2017 21:35
Show Gist options
  • Save malomarrec/a119c5f7f392778d6ad281f5847fc198 to your computer and use it in GitHub Desktop.
Save malomarrec/a119c5f7f392778d6ad281f5847fc198 to your computer and use it in GitHub Desktop.
read.spss_wrapper <- function (path, verbose = FALSE){
#In R 3.4.0, reading SPSS with foreign::read.spss raises errors when converting int factor levels to their labels with use.value.labels=TRUE
#This is a workaround.
list_data <- read.spss(path, to.data.frame=FALSE, use.value.labels=FALSE)
#list_data <- read.sav(path, to.data.frame=FALSE, use.value.labels=TRUE)
l1 = length(list_data)
indx <- sapply(list_data, is.factor)
list_data[indx] <- lapply(list_data[indx], function(x) {
levels(x) <- make.unique(levels(x))
x })
l2 = length(list_data)
if(verbose && l1 != l2){
print("Warning in read.spss_wrapper: unique list is not the same length as original list")
}
return(list_data)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment