[R] SPSS and R ? do they like each other?

Michael Reinecke Reinecke at consultic.com
Thu Jan 12 14:27:36 CET 2006


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:
>  
> Thanks again for your answer! I tried it out. write.foreign produces SPSS syntax, but unfortunally this syntax tells SPSS to take the names (and not the labels) in order to produce SPSS variable labels. The former labels get lost.
> 
> I tried a data frame produced by read.spss and one by spss.get. Here is the read.spss one (the labels meant to be exported are called "Text 1", ...):
> 
> jjread<-  read.spss("test2.sav", use.value.labels=TRUE, 
> to.data.frame=TRUE)
> 
>>str(jjread)
> 
> `data.frame':   30 obs. of  3 variables:
>  $ VAR00001: num  101 102 103 104 105 106 107 108 109 110 ...
>  $ VAR00002: num  6 6 5 6 6 6 6 6 6 6 ...
>  $ VAR00003: num  0 0 6 7 0 7 0 0 0 8 ...
>  - attr(*, "variable.labels")= Named chr  "Text 1" "Text2" "text 3"
>   ..- attr(*, "names")= chr  "VAR00001" "VAR00002" "VAR00003"
> 
>>     datafile<-tempfile()
>>     codefile<-tempfile()
>>     write.foreign(jjread,datafile,codefile,package="SPSS")
>>     file.show(datafile)
>>     file.show(codefile)
> 
> 
> 
> The syntax file I get is:
> 
> DATA LIST FILE= 
> "C:\DOKUME~1\reinecke\LOKALE~1\Temp\Rtmp15028\file27910"  free / VAR00001 VAR00002 VAR00003  .
> 
> VARIABLE LABELS
> VAR00001 "VAR00001" 
>  VAR00002 "VAR00002" 
>  VAR00003 "VAR00003" 
>  .
> 
> EXECUTE.
> 
> 
> I am working on R 2.2.0. But I think a newer version won ´t fix it either, will it?

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




More information about the R-help mailing list