The R Project SVN R

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
727 hornik 1
\name{write.table}
2
\alias{write.table}
3
\title{Data Output}
7212 hornik 4
\description{
5
  \code{write.table} prints its required argument \code{x} (after
6
  converting it to a data frame if it is not one already) to
7
  \code{file}.  The entries in each line (row) are separated by the
8
  value of \code{sep}.
9
}
727 hornik 10
\usage{
11
write.table(x, file = "", append = FALSE, quote = TRUE, sep = " ",
10217 pd 12
            eol = "\n", na = "NA", dec = ".", row.names = TRUE,
11404 hornik 13
	    col.names = TRUE, qmethod = c("escape", "double"))
727 hornik 14
}
15
\arguments{
16
  \item{x}{the object to be written, typically a data frame.  If not, it
11230 ripley 17
    is attempted to coerce \code{x} to a data frame.}
13512 hornik 18
  \item{file}{either a character string naming a file or a connection.
19
    \code{""} indicates output to the console.}
20
  \item{append}{logical.  If \code{TRUE}, the output is appended to the
21
    file.  If \code{FALSE}, any existing file of the name is destroyed.}
27041 ripley 22
  \item{quote}{a logical value or a numeric vector.  If \code{TRUE}, any
13512 hornik 23
    character or factor columns will be surrounded by double quotes.  If
24
    a numeric vector, its elements are taken as the indices of the
27041 ripley 25
    columns to quote.  In both cases, row and column names
26
    are quoted if they are written. If \code{FALSE}, nothing is quoted.}
13512 hornik 27
  \item{sep}{the field separator string.  Values within each row of
28
    \code{x} are separated by this string.}
11404 hornik 29
  \item{eol}{the character(s) to print at the end of each line (row).}
727 hornik 30
  \item{na}{the string to use for missing values in the data.}
11404 hornik 31
  \item{dec}{the string to use for decimal points.}
13512 hornik 32
  \item{row.names}{either a logical value indicating whether the row
33
    names of \code{x} are to be written along with \code{x}, or a
34
    character vector of row names to be written.}
35
  \item{col.names}{either a logical value indicating whether the column
36
    names of \code{x} are to be written along with \code{x}, or a
37
    character vector of column names to be written.}
11404 hornik 38
  \item{qmethod}{a character string specifying how to deal with embedded
11406 ripley 39
    double quote characters when quoting strings.  Must be one of
11404 hornik 40
    \code{"escape"} (default), in which case the quote character is
41
    escaped in C style by a backslash, or \code{"double"}, in which case
42
    it is doubled.  You can specify just the initial letter.}
727 hornik 43
}
8771 ripley 44
\details{
13512 hornik 45
  Normally there is no column name for a column of row names.  If
46
  \code{col.names=NA} a blank column name is added.  This can be used to
47
  write CSV files for input to spreadsheets.
17614 ripley 48
 
49
  \code{write.table} can be slow for data frames with large numbers
50
  (hundreds or more) of columns: this is inevitable as each column could
51
  be of a different class and so must be handled separately.
25118 hornik 52
  Function \code{\link[MASS]{write.matrix}} in package \pkg{MASS}
17614 ripley 53
  may be much more efficient if \code{x} is a matrix or can be
54
  represented in a numeric matrix.
8771 ripley 55
}
11233 ripley 56
 
727 hornik 57
\seealso{
25118 hornik 58
  The \dQuote{R Data Import/Export} manual.
12976 pd 59
 
11230 ripley 60
  \code{\link{read.table}}, \code{\link{write}}.
17614 ripley 61
 
62
  \code{\link[MASS]{write.matrix}}.
727 hornik 63
}
11233 ripley 64
 
65
\examples{
8771 ripley 66
\dontrun{
67
## To write a CSV file for input to Excel one might use
11404 hornik 68
write.table(x, file = "foo.csv", sep = ",", col.names = NA)
8771 ripley 69
## and to read this file back into R one needs
11404 hornik 70
read.table("file.csv", header = TRUE, sep = ",", row.names=1)
8771 ripley 71
}}
7212 hornik 72
\keyword{print}
73
\keyword{file}