Importing values and labels from SPSS with memisc

你说的曾经没有我的故事 提交于 2019-12-10 11:41:02

问题


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


来源:https://stackoverflow.com/questions/19914962/importing-values-and-labels-from-spss-with-memisc

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!