信息从“label属性”在R到“VARIABLE LABELS”在SPSS中 [英] information from `label attribute` in R to `VARIABLE LABELS` in SPSS

查看:207
本文介绍了信息从“label属性”在R到“VARIABLE LABELS”在SPSS中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在R工作,但是我需要以SPSS格式提供一些数据,包括'变量标签'和'值标签',我有点卡住了。



我已经使用 Hmisc 标签添加了变量标签/ code>函数。将变量标签添加为标签属性,当使用 describe() Hmisc 包。问题是我不能从 foreign 包中得到 write.foreign()函数,以识别这些标签作为变量标签。我想我需要修改 write.foreign()使用标签属性作为变量标签写入 .sps 文件。



我看了R列表,在stackoverflow,但我只能找到从2006年的R关于从R 向SPSS导出变量标签的列表,它似乎没有回答我的问题。



这是我的工作示例,

 #首先我创建一个虚拟数据集
df< - data.frame(id = c(1:6),p.code = c(1,5,4,NA,0,5),
p.label = c('视光师','护士','金融分析师',
'< NA& '0','护士'),foo = LETTERS [1:6])

#其次,我使用Hmisc包中的标签添加一些变量标签
#install.packages(' Hmisc',dependencies = TRUE)
库(Hmisc)
label(d f)< - 甜甜数据
label(df $ id)< - id!@#$%^
label(df $ p.label)< - 人类可读的信息
label(df $ p.code) - 专业代码
label(df $ foo)< - 变量x.var的变量标签
#修改一个varibes的名称,只是为了看看导出时会发生什么。
名称(df)[4]< - 新的疯狂名称为'foo'

#第三我从外国包中以write.foreign导出数据
# install.packages('foreign',dependencies = TRUE)
setwd('C:\\\temp')
库(外部)
write.foreign(df,df.wf .txt,df.wf.sps,package =SPSS)

list.files()
[1]df.wf.spsdf.wf. txt

当我检查 .sps 文件(见下面的'df.wf.sps'的内容)我的变量标签与我的变量名称相同除了我更名为foo的新疯狂名字的foo之外。这个变量有一个新的和似乎是随机的名字,但正确的变量标签。



有谁知道如何获得标签属性和导出为变量标签和标签名称的变量名称转换为 .sps 文件?也许有一个更聪明的方式存储'变量标签',然后我现在的方法?



任何帮助将不胜感激。



谢谢,Eric



使用 write.foreign 导出的'df.wf.sps'内容 foreign package

  DATA LIST FILE =df.wf.txt free(,)
/ id p.code p.label Nwcnf.f。 。

VARIABLE LABELS
idid
p.codep.code
p.labelp.label
Nwcnf.f。 新的疯狂名字为'foo'


价值标签
/
p.label
10
2财务分析师
3护士
4验光师
/
Nwcnf.f。
1A
2B
3C
4D
5E
6F


EXECUTE。



更新2012年4月16日15:54:24 PDT;



我正在寻找的是一种调整 write.foreign 的方式来写一个 .sps 这个部分的文件

  [...] 

可变标签
idid
p.codep.code
p.labelp.label
Nwcnf.f。 新的疯狂的名字为'foo'

[...]

这样,

  [...] 

VARIABLE LABELS
idid!@#
p.code专业代码
p.label具有人类可读信息的专业
新的疯狂名称为foo新的疯狂名称为'foo'

[...]

最后一行有点雄心勃勃,我真的不需要在名称中带有空格的变量,但是我希望将标签属性转移到.spas文件(我用R生成)。

解决方案

尝试此功能,看看它是否适用于您。如果没有,添加评论,我可以看到我可以做什么,只要故障排除。

 #步骤1:备份您的数据,以防
df.orig = df
#步骤2:加载以下函数
get.var.labels = function(data){
a = do .call(llist,data)
tempout = vector(list,length(a))

for(i in 1:length(a)){
tempout [ [i]] = label(a [[i]])
}
b = unlist(tempout)
结构(c(b),.Names =名称(数据))
}
#步骤3:应用变量.label属性
属性(df)$ variable.labels = get.var.labels(df)
#步骤4:加载write.SPSS功能可从
#https://stat.ethz.ch/pipermail/r-help/2006-January/085941.html
#步骤5:编写您的SPSS数据文件和代码文件
写.SPSS(df,df.sav,df.sps)

上面的例子是假设您的数据命名为 df ,并且您使用 Hmisc 添加标签,如您所述在你的问题。



更新:自包含的功能



如果您不想更改原始文件,以上示例,如果您在使用此功能时连接到互联网,则可以尝试使用此自包含的功能:

  write.Hmisc.SPSS = function(data,datafile,codefile){
a = do.call(llist,data)
tempout = vector(list,length(a))

for(i in 1:length(a)){
tempout [[i]] = label(a [[i]])
}
b = unlist(tempout )
label.temp = structure(c(b),.Names = names(data))
属性(数据)$ variable.labels = label.temp
source(http: /dl.dropbox.com/u/2556524/R%20Functions/writeSPSS.R)
write.SPSS(data,datafile,codefile)
}

使用简单:

  write.Hmisc。 SPSS(df,df.sav,df.sps)


I'm working in R, but I need to deliver some data in SPSS format with both 'variable labels' and 'value labels' and I'm kinda stuck.

I've added variable labels to my data using the Hmisc's label function. This add the variable labels as a label attribute, which is handy when using describe() from the Hmisc package. The problem is that I cannot get the write.foreign() function, from the foreign package, to recognize these labels as variable labels. I imagine I need to modify write.foreign() to use the label attribute as variable label when writing the .sps file.

I looked at the R list and at stackoverflow, but I could only find a post from 2006 on the R list regarding exporting varibles labels to SPSS from R and it doesn't seem to answer my question.

Here is my working example,

# First I create a dummy dataset
df <- data.frame(id = c(1:6), p.code = c(1, 5, 4, NA, 0, 5),  
                 p.label = c('Optometrists', 'Nurses', 'Financial analysts',
                 '<NA>', '0', 'Nurses'), foo = LETTERS[1:6])

# Second, I add some variable labels using label from the Hmisc package
# install.packages('Hmisc', dependencies = TRUE)
library(Hmisc)
label(df) <- "Sweet sweet data"
label(df$id) <- "id !@#$%^" 
label(df$p.label) <- "Profession with human readable information"
label(df$p.code) <- "Profession code"
label(df$foo) <- "Variable label for variable x.var"
# modify the name of one varibes, just to see what happens when exported.
names(df)[4] <- "New crazy name for 'foo'"

# Third I export the data with write.foreign from the foreign package
# install.packages('foreign', dependencies = TRUE)
setwd('C:\\temp')
library(foreign)
write.foreign(df,"df.wf.txt","df.wf.sps",  package="SPSS")

list.files()
[1] "df.wf.sps" "df.wf.txt"

When I inspect the .sps file (see the content of 'df.wf.sps' below) my variable labels are identical to my variable names, except for foo that I renamed to "New crazy name for 'foo'." This variable has a new and seemly random name, but the correct variable label.

Does anyone know how to get the label attributes and the variable names exported as 'variable labels' and 'labels names' into a .sps file? Maybe there is a smarter way to store 'variable labels' then my current method?

Any help would be greatly appreciated.

Thanks, Eric

Content of 'df.wf.sps' export using write.foreign from the foreign package

DATA LIST FILE= "df.wf.txt"  free (",")
/ id p.code p.label Nwcnf.f.  .

VARIABLE LABELS
 id "id" 
 p.code "p.code" 
 p.label "p.label" 
 Nwcnf.f. "New crazy name for 'foo'" 
 .

VALUE LABELS
/
p.label  
 1 "0" 
 2 "Financial analysts" 
 3 "Nurses" 
 4 "Optometrists" 
/
Nwcnf.f.  
 1 "A" 
 2 "B" 
 3 "C" 
 4 "D" 
 5 "E" 
 6 "F" 
.

EXECUTE.

Update April 16 2012 at 15:54:24 PDT;

What I am looking for is a way to tweak write.foreign to write a .sps file where this part,

[…] 

VARIABLE LABELS
 id "id" 
 p.code "p.code" 
 p.label "p.label" 
 Nwcnf.f. "New crazy name for 'foo'" 

[…] 

looks like this,

[…] 

VARIABLE LABELS
 id "id !@#$%^" 
 p.code "Profession code" 
 p.label "Profession with human readable information" 
 "New crazy name for 'foo'" "New crazy name for 'foo'" 

[…]

The last line is a bit ambitious, I don't really need to have a variables with white spaces in the names, but I would like the label attributes to be transferred to the .spas file (that I produce with R).

解决方案

Try this function and see if it works for you. If not, add a comment and I can see what I can do as far as troubleshooting goes.

# Step 1: Make a backup of your data, just in case
df.orig = df
# Step 2: Load the following function
get.var.labels = function(data) {
  a = do.call(llist, data)
  tempout = vector("list", length(a))

  for (i in 1:length(a)) {
    tempout[[i]] = label(a[[i]])
  }
  b = unlist(tempout)
  structure(c(b), .Names = names(data))
}
# Step 3: Apply the variable.label attributes
attributes(df)$variable.labels = get.var.labels(df)
# Step 4: Load the write.SPSS function available from
# https://stat.ethz.ch/pipermail/r-help/2006-January/085941.html
# Step 5: Write your SPSS datafile and codefile
write.SPSS(df, "df.sav", "df.sps")

The above example is assuming that your data is named df, and you have used Hmisc to add labels, as you described in your question.

Update: A Self-Contained Function

If you do not want to alter your original file, as in the example above, and if you are connected to the internet while you are using this function, you can try this self-contained function:

write.Hmisc.SPSS = function(data, datafile, codefile) {
  a = do.call(llist, data)
  tempout = vector("list", length(a))

  for (i in 1:length(a)) {
    tempout[[i]] = label(a[[i]])
  }
  b = unlist(tempout)
  label.temp = structure(c(b), .Names = names(data))
  attributes(data)$variable.labels = label.temp
  source("http://dl.dropbox.com/u/2556524/R%20Functions/writeSPSS.R")
  write.SPSS(data, datafile, codefile)
}

Usage is simple:

write.Hmisc.SPSS(df, "df.sav", "df.sps")

这篇关于信息从“label属性”在R到“VARIABLE LABELS”在SPSS中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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