Skip to content

SPSS and R ? do they like each other?

3 messages · Michael Reinecke, Chuck Cleland, ronggui

#
Thank you very much! write.SPSS works fine.

I just wonder, why this very useful function is not part of any package. I have not even found it in the web. For experts it may not be a big deal to write their own export functions, but for newcomers like me it is almost impossible - and at the same time it is essential to us to have good facilities for exchange with our familiar statistical package. I think a lack of exchange tools might be something that scares many people off and keeps them from getting to know R.

Well, just do give a greenhorn ??s perspective.

Best regards,

Michael


-----Urspr??ngliche Nachricht-----
Von: Chuck Cleland [mailto:ccleland at optonline.net] 
Gesendet: Donnerstag, 12. Januar 2006 01:16
An: Michael Reinecke
Cc: R-help at stat.math.ethz.ch
Betreff: Re: [R] SPSS and R ? do they like each other?
Michael Reinecke wrote:
Here is a functiong based on modifying foreign:::writeForeignSPSS (by Thomas Lumley) which might work for you:

write.SPSS <- function (df, datafile, codefile, varnames = NULL) { adQuote <- function(x){paste("\"", x, "\"", sep = "")}
     dfn <- lapply(df, function(x) if (is.factor(x))
         as.numeric(x)
     else x)
     write.table(dfn, file = datafile, row = FALSE, col = FALSE)
     if(is.null(attributes(df)$variable.labels)) varlabels <- names(df) else varlabels <- attributes(df)$variable.labels
     if (is.null(varnames)) {
         varnames <- abbreviate(names(df), 8)
         if (any(sapply(varnames, nchar) > 8))
             stop("I cannot abbreviate the variable names to eight or fewer letters")
         if (any(varnames != names(df)))
             warning("some variable names were abbreviated")
     }
     cat("DATA LIST FILE=", dQuote(datafile), " free\n", file = codefile)
     cat("/", varnames, " .\n\n", file = codefile, append = TRUE)
     cat("VARIABLE LABELS\n", file = codefile, append = TRUE)
     cat(paste(varnames, adQuote(varlabels), "\n"), ".\n", file = codefile,
         append = TRUE)
     factors <- sapply(df, is.factor)
     if (any(factors)) {
         cat("\nVALUE LABELS\n", file = codefile, append = TRUE)
         for (v in which(factors)) {
             cat("/\n", file = codefile, append = TRUE)
             cat(varnames[v], " \n", file = codefile, append = TRUE)
             levs <- levels(df[[v]])
             cat(paste(1:length(levs), adQuote(levs), "\n", sep = " "),
                 file = codefile, append = TRUE)
         }
         cat(".\n", file = codefile, append = TRUE)
     }
     cat("\nEXECUTE.\n", file = codefile, append = TRUE) }

--
Chuck Cleland, Ph.D.
NDRI, Inc.
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 452-1424 (M, W, F)
fax: (917) 438-0894
#
Michael Reinecke wrote:
The tool for exporting to SPSS *is* available in the foreign package 
thanks to Thomas Lumley.  I just made a *small modification* to use the 
variable.labels attribute of a data frame if it's available and the 
names of the data frame if that attribute is not available.  Maybe 
Thomas will consider making a change to foreign:::writeForeignSPSS along 
those lines.

Chuck Cleland

  
    
#
2006/1/12, Chuck Cleland <ccleland at optonline.net>:
I agree with this point. it 's usefull when one get the spss data file
into R to do something and export that data back to spss data file.
--
é»„è£è´µ
Deparment of Sociology
Fudan University