EVOLUTION-MANAGER
Edit File: write.table.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Data Output</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for write.table {utils}"><tr><td>write.table {utils}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Data Output</h2> <h3>Description</h3> <p><code>write.table</code> prints its required argument <code>x</code> (after converting it to a data frame if it is not one nor a matrix) to a file or <a href="../../base/html/connections.html">connection</a>. </p> <h3>Usage</h3> <pre> write.table(x, file = "", append = FALSE, quote = TRUE, sep = " ", eol = "\n", na = "NA", dec = ".", row.names = TRUE, col.names = TRUE, qmethod = c("escape", "double"), fileEncoding = "") write.csv(...) write.csv2(...) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>x</code></td> <td> <p>the object to be written, preferably a matrix or data frame. If not, it is attempted to coerce <code>x</code> to a data frame.</p> </td></tr> <tr valign="top"><td><code>file</code></td> <td> <p>either a character string naming a file or a <a href="../../base/html/connections.html">connection</a> open for writing. <code>""</code> indicates output to the console.</p> </td></tr> <tr valign="top"><td><code>append</code></td> <td> <p>logical. Only relevant if <code>file</code> is a character string. If <code>TRUE</code>, the output is appended to the file. If <code>FALSE</code>, any existing file of the name is destroyed.</p> </td></tr> <tr valign="top"><td><code>quote</code></td> <td> <p>a logical value (<code>TRUE</code> or <code>FALSE</code>) or a numeric vector. If <code>TRUE</code>, 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</code>, nothing is quoted.</p> </td></tr> <tr valign="top"><td><code>sep</code></td> <td> <p>the field separator string. Values within each row of <code>x</code> are separated by this string.</p> </td></tr> <tr valign="top"><td><code>eol</code></td> <td> <p>the character(s) to print at the end of each line (row). For example, <code>eol = "\r\n"</code> will produce Windows' line endings on a Unix-alike OS, and <code>eol = "\r"</code> will produce files as expected by Excel:mac 2004.</p> </td></tr> <tr valign="top"><td><code>na</code></td> <td> <p>the string to use for missing values in the data.</p> </td></tr> <tr valign="top"><td><code>dec</code></td> <td> <p>the string to use for decimal points in numeric or complex columns: must be a single character.</p> </td></tr> <tr valign="top"><td><code>row.names</code></td> <td> <p>either a logical value indicating whether the row names of <code>x</code> are to be written along with <code>x</code>, or a character vector of row names to be written.</p> </td></tr> <tr valign="top"><td><code>col.names</code></td> <td> <p>either a logical value indicating whether the column names of <code>x</code> are to be written along with <code>x</code>, or a character vector of column names to be written. See the section on ‘CSV files’ for the meaning of <code>col.names = NA</code>.</p> </td></tr> <tr valign="top"><td><code>qmethod</code></td> <td> <p>a character string specifying how to deal with embedded double quote characters when quoting strings. Must be one of <code>"escape"</code> (default for <code>write.table</code>), in which case the quote character is escaped in C style by a backslash, or <code>"double"</code> (default for <code>write.csv</code> and <code>write.csv2</code>), in which case it is doubled. You can specify just the initial letter.</p> </td></tr> <tr valign="top"><td><code>fileEncoding</code></td> <td> <p>character string: if non-empty declares the encoding to be used on a file (not a connection) so the character data can be re-encoded as they are written. See <code><a href="../../base/html/connections.html">file</a></code>.</p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p>arguments to <code>write.table</code>: <code>append</code>, <code>col.names</code>, <code>sep</code>, <code>dec</code> and <code>qmethod</code> cannot be altered. </p> </td></tr> </table> <h3>Details</h3> <p>If the table has no columns the rownames will be written only if <code>row.names = TRUE</code>, and <em>vice versa</em>. </p> <p>Real and complex numbers are written to the maximal possible precision. </p> <p>If a data frame has matrix-like columns these will be converted to multiple columns in the result (<em>via</em> <code><a href="../../base/html/matrix.html">as.matrix</a></code>) and so a character <code>col.names</code> or a numeric <code>quote</code> should refer to the columns in the result, not the input. Such matrix-like columns are unquoted by default. </p> <p>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</code> 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. </p> <p>Only columns which have been converted to character will be quoted if specified by <code>quote</code>. </p> <p>The <code>dec</code> 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><a href="../../base/html/AsIs.html">I</a>()</code>. Use <code><a href="../../base/html/options.html">options</a>("OutDec")</code> to control such conversions. </p> <p>In almost all cases the conversion of numeric quantities is governed by the option <code>"scipen"</code> (see <code><a href="../../base/html/options.html">options</a></code>), but with the internal equivalent of <code>digits = 15</code>. For finer control, use <code><a href="../../base/html/format.html">format</a></code> to make a character matrix/data frame, and call <code>write.table</code> on that. </p> <p>These functions check for a user interrupt every 1000 lines of output. </p> <p>If <code>file</code> is a non-open connection, an attempt is made to open it and then close it after use. </p> <p>To write a Unix-style file on Windows, use a binary connection e.g. <code>file = file("filename", "wb")</code>. </p> <h3>CSV files</h3> <p>By default there is no column name for a column of row names. If <code>col.names = NA</code> and <code>row.names = TRUE</code> a blank column name is added, which is the convention used for CSV files to be read by spreadsheets. Note that such CSV files can be read in <span style="font-family: Courier New, Courier; color: #666666;"><b>R</b></span> by </p> <pre> read.csv(file = "<filename>", row.names = 1)</pre> <p><code>write.csv</code> and <code>write.csv2</code> provide convenience wrappers for writing CSV files. They set <code>sep</code> and <code>dec</code> (see below), <code>qmethod = "double"</code>, and <code>col.names</code> to <code>NA</code> if <code>row.names = TRUE</code> (the default) and to <code>TRUE</code> otherwise. </p> <p><code>write.csv</code> uses <code>"."</code> for the decimal point and a comma for the separator. </p> <p><code>write.csv2</code> uses a comma for the decimal point and a semicolon for the separator, the Excel convention for CSV files in some Western European locales. </p> <p>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>append</code>, <code>col.names</code>, <code>sep</code>, <code>dec</code> or <code>qmethod</code> are ignored, with a warning. </p> <p>CSV files do not record an encoding, and this causes problems if they are not ASCII for many other applications. Windows Excel 2007/10 will open files (e.g., by the file association mechanism) correctly if they are ASCII or UTF-16 (use <code>fileEncoding = "UTF-16LE"</code>) or perhaps in the current Windows codepage (e.g., <code>"CP1252"</code>), but the ‘Text Import Wizard’ (from the ‘Data’ tab) allows far more choice of encodings. Excel:mac 2004/8 can <em>import</em> only ‘Macintosh’ (which seems to mean Mac Roman), ‘Windows’ (perhaps Latin-1) and ‘PC-8’ files. OpenOffice 3.x asks for the character set when opening the file. </p> <p>There is an IETF RFC4180 (<a href="https://tools.ietf.org/html/rfc4180">https://tools.ietf.org/html/rfc4180</a>) for CSV files, which mandates comma as the separator and CRLF line endings. <code>write.csv</code> writes compliant files on Windows: use <code>eol = "\r\n"</code> on other platforms. </p> <h3>Note</h3> <p><code>write.table</code> 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. </p> <h3>See Also</h3> <p>The ‘R Data Import/Export’ manual. </p> <p><code><a href="read.table.html">read.table</a></code>, <code><a href="../../base/html/write.html">write</a></code>. </p> <p><code><a href="../../MASS/html/write.matrix.html">write.matrix</a></code> in package <a href="https://CRAN.R-project.org/package=MASS"><span class="pkg">MASS</span></a>. </p> <h3>Examples</h3> <pre> ## 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") ## To write a file in Mac Roman for simple use in Mac Excel 2004/8 write.csv(x, file = "foo.csv", fileEncoding = "macroman") ## or for Windows Excel 2007/10 write.csv(x, file = "foo.csv", fileEncoding = "UTF-16LE") ## End(Not run)</pre> <hr /><div style="text-align: center;">[Package <em>utils</em> version 3.6.0 <a href="00Index.html">Index</a>]</div> </body></html>