Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

write.foreign

Write Text Files and Code to Read Them


Description

This function exports simple data frames to other statistical packages by writing the data as free-format text and writing a separate file of instructions for the other package to read the data.

Usage

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

Arguments

df

A data frame

datafile

Name of file for data output

codefile

Name of file for code output

package

Name of package

...

Other arguments for the individual writeForeign functions

Details

The work for this function is done by foreign:::writeForeignStata, foreign:::writeForeignSAS and foreign:::writeForeignSPSS. To add support for another package, eg Systat, create a function writeForeignSystat with the same first three arguments as write.foreign. This will be called from write.foreign when package="Systat".

Numeric variables and factors are supported for all packages: dates and times (Date, dates, date, and POSIXt classes) and logical vectors are also supported for SAS and characters are supported for SPSS.

For package="SAS" there are optional arguments dataname = "rdata" taking a string that will be the SAS data set name, validvarname taking either "V6" or "V7", and libpath = NULL taking a string that will be the directory where the target SAS datset will be written when the generated SAS code been run.

For package="SPSS" there is an optional argument maxchars = 32L taking an integer that causes the variable names (not variable labels) to be abbreviated to not more than maxchars chars. For compatibility with SPSS version 12 and before, change this to maxchars = 8L. In single byte locales with SPSS versions 13 or later, this can be set to maxchars = 64L.

For package="SPSS", as a side effect, the decimal indicator is always set by SET DECIMAL=DOT. which may override user settings of the indicator or its default derived from the current locale.

Value

Invisible NULL.

Author(s)

Thomas Lumley and Stephen Weigand

Examples

## Not run: 
datafile <- tempfile()
codefile <- tempfile()
write.foreign(esoph, datafile, codefile, package="SPSS")
file.show(datafile)
file.show(codefile)
unlink(datafile)
unlink(codefile)

## End(Not run)

foreign

Read Data Stored by 'Minitab', 'S', 'SAS', 'SPSS', 'Stata', 'Systat', 'Weka', 'dBase', ...

v0.8-81
GPL (>= 2)
Authors
R Core Team [aut, cph, cre], Roger Bivand [ctb, cph], Vincent J. Carey [ctb, cph], Saikat DebRoy [ctb, cph], Stephen Eglen [ctb, cph], Rajarshi Guha [ctb, cph], Swetlana Herbrandt [ctb], Nicholas Lewin-Koh [ctb, cph], Mark Myatt [ctb, cph], Michael Nelson [ctb], Ben Pfaff [ctb], Brian Quistorff [ctb], Frank Warmerdam [ctb, cph], Stephen Weigand [ctb, cph], Free Software Foundation, Inc. [cph]
Initial release
2020-12-22

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.