\HeaderA{write.table}{Data Output}{write.table}
\aliasA{write.csv}{write.table}{write.csv}
\aliasA{write.csv2}{write.table}{write.csv2}
\keyword{print}{write.table}
\keyword{file}{write.table}
\begin{Description}\relax
\code{write.table} prints its required argument \code{x} (after
converting it to a data frame if it is not one nor a matrix) to
a file or connection.
\end{Description}
\begin{Usage}
\begin{verbatim}
write.table(x, file = "", append = FALSE, quote = TRUE, sep = " ",
            eol = "\n", na = "NA", dec = ".", row.names = TRUE,
            col.names = TRUE, qmethod = c("escape", "double"))

write.csv(...)
write.csv2(...)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{x}] the object to be written, preferably a matrix or data frame.
If not, it is attempted to coerce \code{x} to a data frame.
\item[\code{file}] either a character string naming a file or a connection
open for writing.  \code{""} indicates output to the console.
\item[\code{append}] logical. Only relevant if \code{file} is a character
string.  If \code{TRUE}, the output is appended to the
file.  If \code{FALSE}, any existing file of the name is destroyed.
\item[\code{quote}] a logical value (\code{TRUE} or \code{FALSE}) or a
numeric vector.  If \code{TRUE}, any character or factor columns
will be surrounded by double quotes.  If a numeric vector, its
elements are taken as the indices of columns to quote.  In both
cases, row and column names are quoted if they are written.  If
\code{FALSE}, nothing is quoted.
\item[\code{sep}] the field separator string.  Values within each row of
\code{x} are separated by this string.
\item[\code{eol}] the character(s) to print at the end of each line (row).
For example, \code{eol="\bsl{}r\bsl{}n"} will produce Windows' line endings on
a Unix-alike OS, and \code{eol="\bsl{}r"} will produce files as expected by
MacOS Excel 2004.
\item[\code{na}] the string to use for missing values in the data.
\item[\code{dec}] the string to use for decimal points in numeric or complex
columns: must be a single character.
\item[\code{row.names}] either a logical value indicating whether the row
names of \code{x} are to be written along with \code{x}, or a
character vector of row names to be written.
\item[\code{col.names}] either a logical value indicating whether the column
names of \code{x} are to be written along with \code{x}, or a
character vector of column names to be written.  See the section on
\sQuote{CSV files} for the meaning of \code{col.names = NA}.
\item[\code{qmethod}] a character string specifying how to deal with embedded
double quote characters when quoting strings.  Must be one of
\code{"escape"} (default), in which case the quote character is
escaped in C style by a backslash, or \code{"double"}, in which case
it is doubled.  You can specify just the initial letter.
\item[\code{...}] arguments to \code{write.table}: \code{col.names},
\code{sep}, \code{dec} and \code{qmethod} cannot be altered.

\end{ldescription}
\end{Arguments}
\begin{Details}\relax
If the table has no columns the rownames will be written only if
\code{row.names=TRUE}, and \emph{vice versa}.

Real and complex numbers are written to the maximal possible precision.

If a data frame has matrix-like columns these will be converted to
multiple columns in the result (\emph{via} \code{\LinkA{as.matrix}{as.matrix}})
and so a character \code{col.names} or a numeric \code{quote} should
refer to the columns in the result, not the input.  Such matrix-like
columns are unquoted by default.

Any columns in a data frame which are lists or have a class
(e.g. dates) will be converted by the appropriate \code{as.character}
method: such columns are unquoted by default.  On the other hand,
any class information for a matrix is discarded and non-atomic
(e.g. list) matrices are coerced to character.

Only columns which have been converted to character will be quoted if
specified by \code{quote}.

The \code{dec} argument only applies to columns that are not subject
to conversion to character because they have a class or are part of a
matrix-like column (or matrix), in particular to columns protected by
\code{\LinkA{I}{I}()}.  Use \code{\LinkA{options}{options}("OutDec")} to control
such conversions.

In almost all cases the conversion of numeric quantities is governed
by the option \code{"scipen"} (see \code{\LinkA{options}{options}}), but with
the internal equivalent of \code{digits=15}.  For finer control, use
\code{\LinkA{format}{format}} to make a character matrix/data frame, and call
\code{write.table} on that.

These functions check for a user interrupt every 1000 lines of output.

If \code{file} is not open for writing, an attempt is made to open it
and then close it after use.
\end{Details}
\begin{Section}{CSV files}
By default there is no column name for a column of row names.  If
\code{col.names = NA} and \code{row.names = TRUE} a blank column name
is added, which is the convention used for CSV files to be read by
spreadsheets.

\code{write.csv} and \code{write.csv2} provide convenience wrappers
for writing CSV files.  They set \code{sep}, \code{dec} and
\code{qmethod}, and \code{col.names} to \code{NA} if \code{row.names =
    TRUE} and \code{TRUE} otherwise.

\code{write.csv} uses \code{"."} for the decimal point and a comma for
the separator.

\code{write.csv2} uses a comma for the decimal point and a semicolon for
the separator, the Excel convention for CSV files in some Western
European locales.

These wrappers are deliberately inflexible: they are designed to
ensure that the correct conventions are used to write a valid file.
Attempts to change \code{col.names}, \code{sep}, \code{dec} or
\code{qmethod} are ignored, with a warning.
\end{Section}
\begin{Note}\relax
\code{write.table} can be slow for data frames with large numbers
(hundreds or more) of columns: this is inevitable as each column could
be of a different class and so must be handled separately.  If they
are all of the same class, consider using a matrix instead.
\end{Note}
\begin{SeeAlso}\relax
The \sQuote{R Data Import/Export} manual.

\code{\LinkA{read.table}{read.table}}, \code{\LinkA{write}{write}}.

\code{\LinkA{write.matrix}{write.matrix}} in package \pkg{MASS}.
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
## Not run: 
## To write a CSV file for input to Excel one might use
x <- data.frame(a = I("a \" quote"), b = pi)
write.table(x, file = "foo.csv", sep = ",", col.names = NA,
            qmethod = "double")
## and to read this file back into R one needs
read.table("foo.csv", header = TRUE, sep = ",", row.names = 1)
## NB: you do need to specify a separator if qmethod = "double".

### Alternatively
write.csv(x, file = "foo.csv")
read.csv("foo.csv", row.names = 1)
## or without row names
write.csv(x, file = "foo.csv", row.names = FALSE)
read.csv("foo.csv")
## End(Not run)\end{ExampleCode}
\end{Examples}

