Importing values and labels from SPSS with memisc

2019-06-06 05:27发布

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")

标签: r spss
2条回答
我命由我不由天
2楼-- · 2019-06-06 06:05

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))
查看更多
Root(大扎)
3楼-- · 2019-06-06 06:09

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.

查看更多
登录 后发表回答