R can also export R data objects (usually data frames and
matrices) as text files. To export data to a text file, use the write.table
function:
write.table(x, file = "", append = FALSE, quote = TRUE, sep = " ", eol = "\n", na = "NA", dec = ".", row.names = TRUE, col.names = TRUE, qmethod = c("escape", "double"))
There are wrapper functions for write.table
that call write.table
with different defaults. These are
useful if you want to create a file of comma-separated values, for
example, to import into Microsoft Excel:
write.csv(...) write.csv2(...)
Here is a description of the arguments to write.table
.
Argument | Description | Default |
---|---|---|
x | Object to export. | |
file | Character value specifying a filename or a connection object to which you would like to write the output. | "" |
append | A logical value indicating whether to append the output to
the end of an existing file (append=TRUE ) or replace the file
(append=FALSE ). | FALSE |
quote | A logical value specifying whether to surround any character or factor values with quotes, or a numeric vector specifying which columns to surround with quotes. | TRUE |
sep | A character value specifying the value that separates values within a row. | "" |
eol | A character value specifying the value to append on the end of each line. | "\n" |
na | A character value specifying how to represent NA values. | "NA" |
dec | A character value specifying the decimal separator in numeric values. | "." |
row.names | A logical value indicating whether to include row names in the output or a numeric vector specifying the rows from which row names should be included. | TRUE |
col.names | A logical value specifying whether to include column names or a character vector specifying alternate names to include. | TRUE |
qmethod | Specifies how to deal with quotes inside quoted character
and factor fields. Specify qmethod="escape" to escape
quotes with a backslash (as in C) or qmethod= "double" to escape quotes as
double quotes (i.e., “ is transformed to “”). | "escape" |