Information from the label attribute in R in `VARIABLE LABELS` in SPSS

I work in R, but I need to deliver some data in SPSS format with "variable labels" and "value signs", and I'm a little stuck.

I added variable labels to my data using a function Hmisc label. This will add variable labels as label attribute, which is convenient when using describe()from the package Hmisc. The problem is that I cannot get the function write.foreign()from the package foreignto recognize these labels as variable labels. It seems to me that I need to change write.foreign()to use label attributeas variable labelwhen writing a file .sps.

I looked at the list of R and in stackoverflow, but I was able to find a message from 2006 on the list of R regarding exporting varibles shortcuts to SPSS from R and it does not 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 check the file .sps(see the contents of "df.wf.sps" below), mine variable labelsis identical to mine variable names, except for foo, which I renamed to "New crazy name for", Foo. " This variable has a new and seemingly random name, but the correct onevariable label.

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

Any help would be greatly appreciated.

,

'df.wf.sps' write.foreign 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.

16 2012 15:54:24 PDT;

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

+5
2

, . , , , , .

# 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 , .

:

, , , :

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

, (), , , , variable.label label.table, SPSS script.

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.table variable.labels, , - row.names = c(NA, -6L), class = "data.frame", .

Update

. write.foreign R. , write.SPSS shared (), , foreign . :

write.SPSS(df, datafile="df.sav", codefile="df.sps")
+1

All Articles