information from `label attribute` in R to `VARIABLE LABELS` in SPSS

本小妞迷上赌 提交于 2019-11-28 07:03:52

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

The function that you linked to (here) should work, but I think the problem is that your dataset doesn't actually have the variable.label and label.table attributes that would be needed to write the SPSS script file.

I don't have access to SPSS, but try the following and see if it at least points you in the right direction. Unfortunately, I don't see an easy way to do this other than editing the output of dput manually.

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)

Compare the above with the output of dput for your sample dataset. Notice that label.table and variable.labels have been added, and a line that said something like row.names = c(NA, -6L), class = "data.frame" was removed.

Update

NOTE: This will not work with the default write.foreign function in R. To test this you first need to load the write.SPSS function shared here, and (of course), make sure that you have the foreign package loaded. Then, you write your files as follows:

write.SPSS(df, datafile="df.sav", codefile="df.sps")
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!