7

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

Hmisc我使用'slabel函数为我的数据添加了变量标签。这会将变量标签添加为 a ,这在从包中label attribute使用时很方便。问题是我无法从包中获得将这些标签识别为变量标签的功能。我想我需要修改以在编写文件时使用as 。describe()Hmiscwrite.foreign()foreignwrite.foreign()label attributevariable label.sps

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

这是我的工作示例,

# 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' 的内容), myvariable labels与 my 相同variable names,除了 foo 我重命名为“'foo' 的新疯狂名称”。这个变量有一个新的看似随机的名字,但是正确的variable label.

有谁知道如何将标签属性和变量名称导出为“变量标签”和“标签名称”到.sps文件中?也许有比我目前的方法更聪明的方法来存储“变量标签”?

任何帮助将不胜感激。

谢谢,埃里克

write.foreign使用从foreign包中导出的“df.wf.sps”的内容

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 生成的文件)。

4

2 回答 2

4

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

# 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")
于 2012-04-21T17:57:17.707 回答
1

您链接到(此处)的函数应该可以工作,但我认为问题在于您的数据集实际上没有编写 SPSS 脚本文件所需的variable.label和属性。label.table

我无法访问 SPSS,但请尝试以下操作,看看它是否至少为您指明了正确的方向。dput不幸的是,除了手动编辑输出之外,我没有看到一个简单的方法来做到这一点。

df = structure(list(id = 1:6, 
               p.code = c(1, 5, 4, NA, 0, 5), 
               p.label = structure(c(5L, 4L, 2L, 3L, 1L, 4L), 
                                   .Label = c("0", "Financial analysts",
                                              "<NA>", "Nurses", 
                                              "Optometrists"), 
                                   class = "factor"), 
               foo = structure(1:6, 
                               .Label = c("A", "B", "C", "D", "E", "F"), 
                               class = "factor")), 
               .Names = c("id", "p.code", "p.label", "foo"),
          label.table = structure(list(id = NULL,
                             p.code = NULL,
                             p.label = structure(c("1", "2", "3", "4", "5"),
                                      .Names = c("0", "Financial analysts", 
                                                 "<NA>", "Nurses", 
                                                 "Optometrists")),
                             foo = structure(1:6, 
                                  .Names = c("A", "B", "C", "D", "E", "F"))),
                             .Names = c("id", "p.code", "p.label", "foo")),
          variable.labels = structure(c("id !@#$%^",  "Profession code", 
                                 "Profession with human readable information",
                                 "New crazy name for 'foo'"), 
                            .Names = c("id", "p.code", "p.label", "foo")), 
          codepage = 65001L)

将上述内容与dput您的示例数据集的输出进行比较。请注意,label.tableandvariable.labels已被添加,并且表示类似内容的行已row.names = c(NA, -6L), class = "data.frame"被删除。

更新

注意:这不适write.foreign用于 R 中的默认函数。要对此进行测试,您首先需要加载这里write.SPSS共享的函数,并且(当然)确保您已加载包。然后,您编写文件如下:foreign

write.SPSS(df, datafile="df.sav", codefile="df.sps")
于 2012-04-17T05:39:04.977 回答