| 42333 |
ripley |
1 |
% File src/library/utils/man/write.table.Rd
|
|
|
2 |
% Part of the R package, http://www.R-project.org
|
| 59039 |
ripley |
3 |
% Copyright 1995-2011 R Core Team
|
| 42333 |
ripley |
4 |
% Distributed under GPL 2 or later
|
|
|
5 |
|
| 57939 |
hornik |
6 |
\newcommand{\CRANpkg}{\href{http://CRAN.R-project.org/package=#1}{\pkg{#1}}}
|
|
|
7 |
|
| 40283 |
ripley |
8 |
\name{write.table}
|
| 56186 |
murdoch |
9 |
\alias{write.table}
|
| 32344 |
ripley |
10 |
\alias{write.csv}
|
|
|
11 |
\alias{write.csv2}
|
| 727 |
hornik |
12 |
\title{Data Output}
|
| 7212 |
hornik |
13 |
\description{
|
|
|
14 |
\code{write.table} prints its required argument \code{x} (after
|
| 32344 |
ripley |
15 |
converting it to a data frame if it is not one nor a matrix) to
|
| 54236 |
ripley |
16 |
a file or \link{connection}.
|
| 7212 |
hornik |
17 |
}
|
| 727 |
hornik |
18 |
\usage{
|
|
|
19 |
write.table(x, file = "", append = FALSE, quote = TRUE, sep = " ",
|
| 10217 |
pd |
20 |
eol = "\n", na = "NA", dec = ".", row.names = TRUE,
|
| 53748 |
ripley |
21 |
col.names = TRUE, qmethod = c("escape", "double"),
|
|
|
22 |
fileEncoding = "")
|
| 32346 |
ripley |
23 |
|
| 34879 |
ripley |
24 |
write.csv(\dots)
|
|
|
25 |
write.csv2(\dots)
|
| 727 |
hornik |
26 |
}
|
|
|
27 |
\arguments{
|
| 32344 |
ripley |
28 |
\item{x}{the object to be written, preferably a matrix or data frame.
|
| 32334 |
ripley |
29 |
If not, it is attempted to coerce \code{x} to a data frame.}
|
| 54236 |
ripley |
30 |
\item{file}{either a character string naming a file or a \link{connection}
|
| 40885 |
ripley |
31 |
open for writing. \code{""} indicates output to the console.}
|
| 42274 |
ripley |
32 |
\item{append}{logical. Only relevant if \code{file} is a character
|
|
|
33 |
string. If \code{TRUE}, the output is appended to the
|
| 13512 |
hornik |
34 |
file. If \code{FALSE}, any existing file of the name is destroyed.}
|
| 38493 |
ripley |
35 |
\item{quote}{a logical value (\code{TRUE} or \code{FALSE}) or a
|
|
|
36 |
numeric vector. If \code{TRUE}, any character or factor columns
|
|
|
37 |
will be surrounded by double quotes. If a numeric vector, its
|
|
|
38 |
elements are taken as the indices of columns to quote. In both
|
|
|
39 |
cases, row and column names are quoted if they are written. If
|
|
|
40 |
\code{FALSE}, nothing is quoted.}
|
| 13512 |
hornik |
41 |
\item{sep}{the field separator string. Values within each row of
|
|
|
42 |
\code{x} are separated by this string.}
|
| 42274 |
ripley |
43 |
\item{eol}{the character(s) to print at the end of each line (row).
|
| 61160 |
ripley |
44 |
For example, \code{eol = "\r\n"} will produce Windows' line endings on
|
|
|
45 |
a Unix-alike OS, and \code{eol = "\r"} will produce files as expected by
|
| 53969 |
ripley |
46 |
Excel:mac 2004.}
|
| 727 |
hornik |
47 |
\item{na}{the string to use for missing values in the data.}
|
| 32344 |
ripley |
48 |
\item{dec}{the string to use for decimal points in numeric or complex
|
|
|
49 |
columns: must be a single character.}
|
| 13512 |
hornik |
50 |
\item{row.names}{either a logical value indicating whether the row
|
|
|
51 |
names of \code{x} are to be written along with \code{x}, or a
|
|
|
52 |
character vector of row names to be written.}
|
|
|
53 |
\item{col.names}{either a logical value indicating whether the column
|
|
|
54 |
names of \code{x} are to be written along with \code{x}, or a
|
| 34879 |
ripley |
55 |
character vector of column names to be written. See the section on
|
| 38493 |
ripley |
56 |
\sQuote{CSV files} for the meaning of \code{col.names = NA}.}
|
| 11404 |
hornik |
57 |
\item{qmethod}{a character string specifying how to deal with embedded
|
| 11406 |
ripley |
58 |
double quote characters when quoting strings. Must be one of
|
| 53969 |
ripley |
59 |
\code{"escape"} (default for \code{write.table}), in which case the
|
|
|
60 |
quote character is escaped in C style by a backslash, or
|
|
|
61 |
\code{"double"} (default for \code{write.csv} and
|
|
|
62 |
\code{write.csv2}), in which case it is doubled. You can specify
|
|
|
63 |
just the initial letter.}
|
| 53748 |
ripley |
64 |
\item{fileEncoding}{character string: if non-empty declares the
|
|
|
65 |
encoding to be used on a file (not a connection) so the character data can
|
|
|
66 |
be re-encoded as they are written. See \code{\link{file}}.}
|
| 32346 |
ripley |
67 |
|
| 50436 |
ripley |
68 |
\item{\dots}{arguments to \code{write.table}: \code{append},
|
|
|
69 |
\code{col.names}, \code{sep}, \code{dec} and \code{qmethod}
|
|
|
70 |
cannot be altered.
|
| 34879 |
ripley |
71 |
}
|
| 727 |
hornik |
72 |
}
|
| 8771 |
ripley |
73 |
\details{
|
| 32344 |
ripley |
74 |
If the table has no columns the rownames will be written only if
|
| 61160 |
ripley |
75 |
\code{row.names = TRUE}, and \emph{vice versa}.
|
| 32335 |
ripley |
76 |
|
|
|
77 |
Real and complex numbers are written to the maximal possible precision.
|
| 32344 |
ripley |
78 |
|
| 32335 |
ripley |
79 |
If a data frame has matrix-like columns these will be converted to
|
|
|
80 |
multiple columns in the result (\emph{via} \code{\link{as.matrix}})
|
|
|
81 |
and so a character \code{col.names} or a numeric \code{quote} should
|
|
|
82 |
refer to the columns in the result, not the input. Such matrix-like
|
|
|
83 |
columns are unquoted by default.
|
| 32344 |
ripley |
84 |
|
|
|
85 |
Any columns in a data frame which are lists or have a class
|
|
|
86 |
(e.g. dates) will be converted by the appropriate \code{as.character}
|
|
|
87 |
method: such columns are unquoted by default. On the other hand,
|
| 37229 |
ripley |
88 |
any class information for a matrix is discarded and non-atomic
|
|
|
89 |
(e.g. list) matrices are coerced to character.
|
| 32344 |
ripley |
90 |
|
| 38493 |
ripley |
91 |
Only columns which have been converted to character will be quoted if
|
|
|
92 |
specified by \code{quote}.
|
|
|
93 |
|
| 32344 |
ripley |
94 |
The \code{dec} argument only applies to columns that are not subject
|
|
|
95 |
to conversion to character because they have a class or are part of a
|
| 37229 |
ripley |
96 |
matrix-like column (or matrix), in particular to columns protected by
|
| 34879 |
ripley |
97 |
\code{\link{I}()}. Use \code{\link{options}("OutDec")} to control
|
|
|
98 |
such conversions.
|
| 35893 |
ripley |
99 |
|
| 37308 |
ripley |
100 |
In almost all cases the conversion of numeric quantities is governed
|
| 40500 |
ripley |
101 |
by the option \code{"scipen"} (see \code{\link{options}}), but with
|
| 61160 |
ripley |
102 |
the internal equivalent of \code{digits = 15}. For finer control, use
|
| 37308 |
ripley |
103 |
\code{\link{format}} to make a character matrix/data frame, and call
|
|
|
104 |
\code{write.table} on that.
|
|
|
105 |
|
| 35893 |
ripley |
106 |
These functions check for a user interrupt every 1000 lines of output.
|
| 40885 |
ripley |
107 |
|
| 54460 |
ripley |
108 |
If \code{file} is a non-open connection, an attempt is made to open it
|
| 42274 |
ripley |
109 |
and then close it after use.
|
| 61433 |
ripley |
110 |
|
| 42274 |
ripley |
111 |
To write a Unix-style file on Windows, use a binary connection
|
| 54460 |
ripley |
112 |
e.g. \code{file = file("filename", "wb")}.
|
| 32335 |
ripley |
113 |
}
|
| 34879 |
ripley |
114 |
\section{CSV files}{
|
|
|
115 |
By default there is no column name for a column of row names. If
|
|
|
116 |
\code{col.names = NA} and \code{row.names = TRUE} a blank column name
|
| 38493 |
ripley |
117 |
is added, which is the convention used for CSV files to be read by
|
| 53969 |
ripley |
118 |
spreadsheets. Note that such CSV files can be read in \R by
|
| 55047 |
ripley |
119 |
\preformatted{ read.csv(file = "<filename>", row.names = 1)}
|
| 34879 |
ripley |
120 |
|
|
|
121 |
\code{write.csv} and \code{write.csv2} provide convenience wrappers
|
| 53969 |
ripley |
122 |
for writing CSV files. They set \code{sep} and \code{dec} (see
|
|
|
123 |
below), \code{qmethod = "double"}, and \code{col.names} to \code{NA}
|
|
|
124 |
if \code{row.names = TRUE} (the default) and to \code{TRUE} otherwise.
|
| 34879 |
ripley |
125 |
|
|
|
126 |
\code{write.csv} uses \code{"."} for the decimal point and a comma for
|
|
|
127 |
the separator.
|
|
|
128 |
|
|
|
129 |
\code{write.csv2} uses a comma for the decimal point and a semicolon for
|
|
|
130 |
the separator, the Excel convention for CSV files in some Western
|
|
|
131 |
European locales.
|
|
|
132 |
|
|
|
133 |
These wrappers are deliberately inflexible: they are designed to
|
|
|
134 |
ensure that the correct conventions are used to write a valid file.
|
| 50436 |
ripley |
135 |
Attempts to change \code{append}, \code{col.names}, \code{sep},
|
|
|
136 |
\code{dec} or \code{qmethod} are ignored, with a warning.
|
| 61433 |
ripley |
137 |
|
| 53755 |
ripley |
138 |
CSV files do not record an encoding, and this causes problems if they
|
|
|
139 |
are not ASCII for many other applications. Windows Excel 2007/10 will
|
| 53782 |
ripley |
140 |
open files (e.g. by the file association mechanism) correctly if they
|
| 53758 |
ripley |
141 |
are ASCII or UTF-16 (use \code{fileEncoding = "UTF-16LE"}) or perhaps in
|
| 53755 |
ripley |
142 |
the current Windows codepage (e.g. \code{"CP1252"}), but the
|
|
|
143 |
\sQuote{Text Import Wizard} (from the \sQuote{Data} tab) allows far
|
| 53758 |
ripley |
144 |
more choice of encodings. Excel:mac 2004/8 can \emph{import} only
|
| 53755 |
ripley |
145 |
Macintosh (which seems to mean MacRoman), Windows (perhaps Latin-1)
|
|
|
146 |
and \sQuote{PC-8} files. OpenOffice 3.x asks for the character set
|
|
|
147 |
when opening the file.
|
| 54460 |
ripley |
148 |
|
|
|
149 |
There is an IETF RFC4180 (\url{http://tools.ietf.org/html/rfc4180})
|
|
|
150 |
for CSV files, which mandates comma as the separator and CRLF line
|
|
|
151 |
endings. \code{write.csv} writes compliant files on Windows: use
|
| 61160 |
ripley |
152 |
\code{eol = "\r\n"} on other platforms.
|
| 34879 |
ripley |
153 |
}
|
| 32344 |
ripley |
154 |
\note{
|
|
|
155 |
\code{write.table} can be slow for data frames with large numbers
|
|
|
156 |
(hundreds or more) of columns: this is inevitable as each column could
|
|
|
157 |
be of a different class and so must be handled separately. If they
|
|
|
158 |
are all of the same class, consider using a matrix instead.
|
|
|
159 |
}
|
| 11233 |
ripley |
160 |
|
| 727 |
hornik |
161 |
\seealso{
|
| 32334 |
ripley |
162 |
The \sQuote{R Data Import/Export} manual.
|
| 31247 |
- |
163 |
|
| 11230 |
ripley |
164 |
\code{\link{read.table}}, \code{\link{write}}.
|
| 17614 |
ripley |
165 |
|
| 57939 |
hornik |
166 |
\code{\link[MASS]{write.matrix}} in package \CRANpkg{MASS}.
|
| 727 |
hornik |
167 |
}
|
| 11233 |
ripley |
168 |
|
|
|
169 |
\examples{
|
| 8771 |
ripley |
170 |
\dontrun{
|
|
|
171 |
## To write a CSV file for input to Excel one might use
|
| 32344 |
ripley |
172 |
x <- data.frame(a = I("a \" quote"), b = pi)
|
|
|
173 |
write.table(x, file = "foo.csv", sep = ",", col.names = NA,
|
|
|
174 |
qmethod = "double")
|
| 8771 |
ripley |
175 |
## and to read this file back into R one needs
|
| 32344 |
ripley |
176 |
read.table("foo.csv", header = TRUE, sep = ",", row.names = 1)
|
|
|
177 |
## NB: you do need to specify a separator if qmethod = "double".
|
|
|
178 |
|
|
|
179 |
### Alternatively
|
|
|
180 |
write.csv(x, file = "foo.csv")
|
|
|
181 |
read.csv("foo.csv", row.names = 1)
|
| 34927 |
ripley |
182 |
## or without row names
|
|
|
183 |
write.csv(x, file = "foo.csv", row.names = FALSE)
|
|
|
184 |
read.csv("foo.csv")
|
| 53748 |
ripley |
185 |
|
| 53755 |
ripley |
186 |
## To write a file in MacRoman for simple use in Mac Excel 2004/8
|
| 53748 |
ripley |
187 |
write.csv(x, file = "foo.csv", fileEncoding = "macroman")
|
| 53755 |
ripley |
188 |
## or for Windows Excel 2007/10
|
| 53758 |
ripley |
189 |
write.csv(x, file = "foo.csv", fileEncoding = "UTF-16LE")
|
| 32344 |
ripley |
190 |
}}
|
| 7212 |
hornik |
191 |
\keyword{print}
|
|
|
192 |
\keyword{file}
|