[R] write.foreign, SPSS on Mac OS X

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Jul 13 19:08:39 CEST 2005

On Wed, 13 Jul 2005, EJ Nikelski wrote:

> Hello,
>     Thanks for your help Brian. You are correct in assuming that I am
> trying to use write.foreign to export a data frame for use in SPSS,
> using the usual format:
> >write.foreign(df, dataFile, codeFile, package="SPSS")
> Your suggestion that the unprintable characters represent UTF-8 encoded
> Unicode left and right double quotes also appears correct. Now, although
> the suggested work-around may well help, the foreign package does seem
> to be creating a corrupted file. That is, an entirely 8-bit ASCII file
> containing embedded UTF-8 double quotes is not valid by any standard --
> and is thus unreadable by any editor on any platform. Perhaps I should

Not true: any editor in a UTF-8 locale should be able to read a valid 
UTF-8 file, and there seems to be a problem with your OS.  No one said 
this had to be an ASCII file, and it will not be if the labels are not 

BTW, `8-bit ASCII' are mutually exclusive terms in file encodings.

> look into filing a bug report on this to the foreign package maintainer.

Which is R-core, and we are already working on a fix.

> Thanks,
> Jim
> Prof Brian Ripley wrote:
>> On Tue, 12 Jul 2005, EJ Nikelski wrote:
>>>     I have jut installed the foreign package (v 0.8-8) on my OS X
>>> machine, and have a bit of a problem writing out a data frame in SPSS
>>> format. Specifically, the code file (the .sps format file) seems to
>>> write 3 unprintable hex values instead of double quotes. For example, in
>>> the following output ...
>>> /
>>> immDel
>>> 1 ###1###
>>>  2 ###2###
>>>  3 ###3###
>>>  ... emacs tells me that the left-sided ### are the hex codes E2 80 9C,
>>> on the right we have E2 80 9D. I am supposing that I should be seeing
>>> double-quotes here? Interestingly, the data file, which also contains a
>>> quoted field, writes out the quotes without any problem. Does anyone
>>> have any ideas?
>> An idea. Those are left and right double quotes in UTF-8 and since MacOS X
>> is usually in a UTF-8 locale they should be printable.  However, I
>> suspect that SPSS is expecting ASCII double quotation marks.
>> You haven't told us what you did, but I guess you used
>> write.foreign(package="SPSS").  That calls writeForeignSPSS which
>> contains calls to dQuote(), and the latter are wrong if ASCII quotation
>> marks are needed.
>> A quick workaround is to use a non-UTF-8 locale: how you do that on ypur
>> OS depends on how you run R so please ask advice on the R-sig-mac list.
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

More information about the R-help mailing list