The R Project SVN R

Rev

Rev 86925 | Details | Compare with Previous | Last modification | View Log | RSS feed

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