r - 从 R 中的 `label attribute` 到 SPSS 中的 `VARIABLE LABELS` 的信息

标签 r export labels spss

我在 R 中工作,但我需要以 SPSS 格式提供一些带有“变量标签”和“值标签”的数据,我有点卡住了。

我已经使用 Hmisc 为我的数据添加了变量标签的 label功能。这将变量标签添加为 label attribute ,这在使用 describe() 时很方便来自 Hmisc包裹。问题是我无法获得 write.foreign()函数,来自 foreign包,将这些标签识别为可变标签。我想我需要修改 write.foreign()使用 label attributevariable label.sps 时文件。

我查看了 R 列表和 stackoverflow,但我只能找到 a post from 2006 on the R list regarding exporting varibles labels to SPSS from R它似乎没有回答我的问题。

这是我的工作示例,

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

当我检查 .sps文件(参见下面“df.wf.sps”的内容)我的 variable labels与我的 variable names 相同,除了我重命名为“'foo'的新疯狂名称”的foo。这个变量有一个新的看似随机的名字,但正确的variable label.
有谁知道如何将标签属性和作为“变量标签”和“标签名称”导出的变量名称获取到 .sps 中文件?也许有比我目前的方法更聪明的存储“变量标签”的方法?

任何帮助将不胜感激。

谢谢,埃里克

使用 write.foreign 导出“df.wf.sps”的内容来自 foreign包裹
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.

2012 年 4 月 16 日太平洋夏令时间 15:54:24 更新;

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

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

[…] 

看起来像这样,
[…] 

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

[…]

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

最佳答案

试试这个功能,看看它是否适合你。如果没有,请添加评论,我可以看到就故障排除而言我可以做些什么。

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

上面的示例假设您的数据名为 df ,并且您已经使用过 Hmisc添加标签,如您在问题中所述。

更新:一个独立的函数

如果您不想更改原始文件,如上例所示,并且在使用此功能时已连接到 Internet,则可以尝试此自包含功能:
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)
}

用法很简单:
write.Hmisc.SPSS(df, "df.sav", "df.sps")

关于r - 从 R 中的 `label attribute` 到 SPSS 中的 `VARIABLE LABELS` 的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10181730/

相关文章:

r - 使用 gsub 提取第一个整数

r - 在 metafor 中为漏斗图点创建标签

matplotlib - 将颜色条的标签从增加值更改为减少值

r - 在ggplot2 boxplot上添加多个标签

r - 如何忽略 future 的浏览器调用

r - R 中 pairwise.t.test 的数据操作

r - 如何清理我的数据并使用 ggplot2 创建图表?

graphics - 以 2D 矢量格式从 Pov-Ray 转换/导出图形

Azure DevOps PDF 导出插件建议

perl - 如何在两个不同的 perl 脚本之间共享/导出全局变量?