Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
R in Action, Second Edition.pdf
Скачиваний:
540
Добавлен:
26.03.2016
Размер:
20.33 Mб
Скачать

appendix C Exporting data from R

In chapter 2, we reviewed a wide range of methods for importing data into R. But sometimes you’ll want to go the other way—exporting data from R—so that data can be archived or imported into external applications. In this appendix, you’ll learn how to output an R object to a delimited text file, an Excel spreadsheet, or a statistical application (such as SPSS, SAS, or Stata).

Delimited text file

You can use the write.table() function to output an R object to a delimited text file. The format is

write.table(x, outfile, sep=delimiter, quote=TRUE, na="NA")

where x is the object and outfile is the target file. For example, the statement

write.table(mydata, "mydata.txt", sep=",")

saves the dataset mydata to a comma-delimited file named mydata.txt in the current working directory. Include a path (for example, "c:/myprojects/mydata.txt") to save the output file elsewhere. Replacing sep="," with sep="\t" saves the data in a tab-delimited file. By default, strings are enclosed in quotes ("") and missing values are written as NA.

Excel spreadsheet

The write.xlsx() function in the xlsx package can be used to save an R data frame to an Excel 2007 workbook. The format is

library(xlsx)

write.xlsx(x, outfile, col.Names=TRUE, row.names=TRUE, sheetName="Sheet 1", append=FALSE)

540

APPENDIX C Exporting data from R

541

For example, the statements

library(xlsx)

write.xlsx(mydata, "mydata.xlsx")

export the data frame mydata to a worksheet (Sheet 1 by default) in an Excel workbook named mydata.xlsx in the current working directory. By default, the variable names in the dataset are used to create column headings in the spreadsheet, and row names are placed in the first column of the spreadsheet. If mydata.xlsx already exists, it’s overwritten.

The xlsx package is a powerful tool for manipulating Excel 2007 workbooks. See the package documentation for more details.

Statistical applications

The write.foreign() function in the foreign package can be used to export a data frame to an external statistical application. Two files are created: a free-format text file containing the data, and a code file containing instructions for reading the data into the external statistical application. The format is

write.foreign(dataframe, datafile, codefile, package=package)

For example, the code

library(foreign)

write.foreign(mydata, "mydata.txt", "mycode.sps", package="SPSS")

exports the dataframe mydata into a free-format text file named mydata.txt in the current working directory and an SPSS program named mycode.sps that can be used to read the text file. Other values of package include "SAS" and "Stata".

To learn more about exporting data from R, see the “R Data Import/Export” documentation, available from http://cran.r-project.org/doc/manuals/R-data.pdf.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]