[英]Extracting labels from SPSS files in R
I imported a SPSS file into R using the haven package. 我使用避风港软件包将SPSS文件导入到R中。 As you can see on the next image, all variables have labels associated with them (eg "2016 YEAR OF ADMINISTRATION"):
如下图所示,所有变量都有与之关联的标签(例如,“ 2016 YEAR OF ADMINISTRATION”):
I'm trying to read those labels, however using this line of code returns NULL: 我正在尝试读取这些标签,但是使用以下代码行将返回NULL:
attr(X36799_0001_Data[,15], "label")
ant this line of code also returns NULL: ant这行代码还返回NULL:
attributes(X36799_0001_Data)$variable.labels
Any info on what I'm doing wrong would be hugely appreciated. 任何关于我做错事情的信息将不胜感激。 Thank you!
谢谢!
Just change the way you are subsetting, and it should work. 只需更改您的设置方式,它就可以工作。
attr(X36799_0001_Data[[15]], "label")
The explanation of this has to do with the way R subsets. 对此的解释与R子集的方式有关。 An in depth explanation is here: Subsetting - Advanced R .
深入的解释在这里: Subsetting-Advanced R。
You can also use the package labelled
to deal with SPSS labels. 您也可以使用包
labelled
处理SPSS标签。 In this case, using the var_label
. 在这种情况下,请使用
var_label
。
var_label(X36799_0001_Data[, 15])
try "labelled" package to get the labels 尝试“标签”包以获取标签
data_label <- t(as.data.frame(labelled::var_label(data))) data_label <-t(as.data.frame(labeled :: var_label(data)))
write.csv(data_label, "data_label.csv") write.csv(data_label,“ data_label.csv”)
Hope this helps 希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.