Exporting Data
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 ... |
Get R in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.