Re: SPSS and R ? do they like each other?

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Re: SPSS and R ? do they like each other?

Michael Reinecke
... and is there also such a nice tool (like spss.get) for exporting
data frames to SPSS? write.table does not keep the data frame labels -
neither did the other exporting tools that I found.
 
Thanks!
 
Michael

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: SPSS and R ? do they like each other?

Chuck Cleland
Michael Reinecke wrote:
> ... and is there also such a nice tool (like spss.get) for exporting
> data frames to SPSS? write.table does not keep the data frame labels -
> neither did the other exporting tools that I found.
> ...

library(foreign)
?write.foreign

write.foreign(df, datafile, codefile, package = "SPSS")

   The codefile generated is SPSS syntax which will read the datafile
and create SPSS variable and value labels.

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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: SPSS and R ? do they like each other?

Michael Reinecke
In reply to this post by Michael Reinecke
 
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?

Greetings,

Michael


-----Ursprüngliche Nachricht-----
Von: Chuck Cleland [mailto:[hidden email]]
Gesendet: Mittwoch, 11. Januar 2006 17:16
An: Michael Reinecke
Cc: [hidden email]
Betreff: Re: [R] SPSS and R ? do they like each other?

Michael Reinecke wrote:
> ... and is there also such a nice tool (like spss.get) for exporting
> data frames to SPSS? write.table does not keep the data frame labels -
> neither did the other exporting tools that I found.
> ...

library(foreign)
?write.foreign

write.foreign(df, datafile, codefile, package = "SPSS")

   The codefile generated is SPSS syntax which will read the datafile and create SPSS variable and value labels.

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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: SPSS and R ? do they like each other?

Thomas Lumley
On Wed, 11 Jan 2006, 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.

Well, yes. That's because write.foreign is basically intended for
exporting R data frame, which don't have variable labels. It should be a
fairly simple change. Look at
   foreign:::writeForeignSPSS
which is the function that does the work.

  -thomas

>
> 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?
>
> Greetings,
>
> Michael
>
>
> -----Ursprüngliche Nachricht-----
> Von: Chuck Cleland [mailto:[hidden email]]
> Gesendet: Mittwoch, 11. Januar 2006 17:16
> An: Michael Reinecke
> Cc: [hidden email]
> Betreff: Re: [R] SPSS and R ? do they like each other?
>
> Michael Reinecke wrote:
>> ... and is there also such a nice tool (like spss.get) for exporting
>> data frames to SPSS? write.table does not keep the data frame labels -
>> neither did the other exporting tools that I found.
>> ...
>
> library(foreign)
> ?write.foreign
>
> write.foreign(df, datafile, codefile, package = "SPSS")
>
>   The codefile generated is SPSS syntax which will read the datafile and create SPSS variable and value labels.
>
> --
> 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
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
Thomas Lumley Assoc. Professor, Biostatistics
[hidden email] University of Washington, Seattle
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: SPSS and R ? do they like each other?

Chuck Cleland
In reply to this post by Michael Reinecke
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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: SPSS and R ? do they like each other?

Michael Reinecke
In reply to this post by Michael Reinecke
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:[hidden email]]
Gesendet: Donnerstag, 12. Januar 2006 01:16
An: Michael Reinecke
Cc: [hidden email]
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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: SPSS and R ? do they like each other?

Chuck Cleland
Michael Reinecke wrote:
> 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.
> ...

   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

> -----Ursprüngliche Nachricht-----
> Von: Chuck Cleland [mailto:[hidden email]]
> Gesendet: Donnerstag, 12. Januar 2006 01:16
> An: Michael Reinecke
> Cc: [hidden email]
> 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
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: SPSS and R ? do they like each other?

Wincent
2006/1/12, Chuck Cleland <[hidden email]>:

> Michael Reinecke wrote:
> > 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.
> > ...
>
>    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.

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.

>
> Chuck Cleland
>
> > -----Ursprüngliche Nachricht-----
> > Von: Chuck Cleland [mailto:[hidden email]]
> > Gesendet: Donnerstag, 12. Januar 2006 01:16
> > An: Michael Reinecke
> > Cc: [hidden email]
> > 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
> >
> > ______________________________________________
> > [hidden email] mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> >
>
> --
> 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
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>


--
黄荣贵
Deparment of Sociology
Fudan University

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html