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.

ArgumentDescriptionDefault
xObject to export. 
fileCharacter value specifying a filename or a connection object to which you would like to write the output.“”
appendA logical value indicating whether to append the output to the end of an existing file (append=TRUE) or replace the file (append=FALSE).FALSE
quoteA 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
sepA character value specifying the value that separates values within a row.“”
eolA character value specifying the value to append on the end of each line.“\n”
naA character value specifying how to represent NA values.“NA”
decA character value specifying the decimal separator in numeric values.“.”
row.namesA 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.