read.spss后如何访问R data.frame列描述 [英] How to access R data.frame column descriptions after read.spss

查看:129
本文介绍了read.spss后如何访问R data.frame列描述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用库外部read.spss函数导入了SPSS .sav文件。

I have imported an SPSS .sav file using the library foreign read.spss function.

dataset = read.spss("data.sav", to.data.frame=TRUE)

我想访问列说明但无法解决如何以编程方式访问它们的问题。我可以在RStudio的数据查看器中的粗体列名称下面的标题中看到这些内容。

I want to access the column descriptions but can't work out how to access them programmatically. I can see these in the data viewer in RStudio, in the header just below the bold column names.

此处的图片: http://i.stack.imgur.com/PgIO5.png

推荐答案

最好使用read_sav 函数导入数据project.org/web/packages/haven/index.html rel = noreferrer> haven 包(Hadley Wickham的另一个很棒的包)。

You may be better off importing the data using the read_sav function from the haven package (another great package from Hadley Wickham).

dd <- read_sav("SomeFile.sav")

head(dd)[,1:10]

methods(as_factor)
table(dd$District)
class(dd$District)
class(dd$Date)
lapply(dd, class)   # some variables have labels and others don't
lapply(dd, class) %>% head



带有标签的变量具有显示其变量标签( label)和它们的值标签(标签)



The 'labelled' variables have attributes to show their variable label ('label') and their value labels ('labels')

dd$Region
attributes(dd$Region)



您可以阅读变量标签:



You can read a variable label:

attr(dd$Region, 'label')



您可以更改变量标签:



You can change a variable label:

attr(dd$Region, 'label') <- 'a new label for Region'
attr(dd$Region, 'label')



与价值标签相同



same for value labels

attr(dd$Region, 'labels')



到chan ge名称,您需要更改属性的名称



to change the names, you need to change the 'names' of the attribute

names(attr(dd$Region, 'labels')) <- c("NE","Nyanza","West")
attr(dd$Region, 'labels')

这篇关于read.spss后如何访问R data.frame列描述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆