Importing values and labels from SPSS with memisc

2019-06-06 06:12发布

问题:

I want to import both values and labels from a dataset but I don't understand how to do it with this package (the documentation is not clear). I know it is possible because Rz (a gui interface for R) uses memisc to do this. I prefer, though, not to depend on too many packages.

Here the only piece of code I have:

dataset <- spss.system.file("file.sav")

回答1:

See the example in ?importer() which covers spss.system.file().

spss.system.file creates an 'importer' object that can show you variable names.

To actually use the data, you need to either do:

## To get the whole file
dataset2 <- as.data.set(dataset)

## To get selected variables
dataset2 <- subset(dataset, select=c(variable names)) to get selected variables.

You end up with a data.set object which is quite complex, but does have what you want. For analysis, you usually need to do: as.data.frame on dataset2.



回答2:

I figured out a solution to this that I like

df <- suppressWarnings(read.spss("C:/Users/yada/yada/yada/ - SPSS_File.sav", to.data.frame = TRUE, use.value.labels = TRUE))

var_labels <- attr(df, "variable.labels")
names <-  data.frame(column = 1:ncol(df), names(df), labels = var_labels, row.names=NULL) 
names(df) <- names$labels 
names(df) <- make.names(df))


标签: r spss