write.table
Write Matrix of Data to a File

Description

Write a matrix or a data frame to a file suitable for importing into other programs.

Usage

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(...)

Arguments

x the object to be written: can be a matrix, a data.frame, or a vector of numeric or character data.
file a character string naming the file to write to. The empty string "" indicates the standard output.
append if TRUE, write.table adds to an existing file; otherwise, it creates a new file and destroys any existing file by that name.
quote a logical value or a numeric vector. If TRUE (the default), any strings in the data, including the row and column names, are surrounded by double quotes. If FALSE, strings are not quoted. If a numeric vector, its elements are taken as the indices of columns to quote.
sep the separator character to use between fields in the file.
eol the character(s) to print at the end of each line(row).
na the character string to use for missing values in the data.
dec This argument is not yet implemented. the character string to use for decimal points in numeric or complex columns. It must be a single character.
row.names either a logical value or a character vector. If logical, TRUE (the default) specifies that the row names of x should be written. If a character vector, the row names to be written.
col.names either a logical value or a character vector. If logical, TRUE (the default) specifies whether the column names of x should be written. If a character vector, the column names to be written.
qmethod a character string. Specifies how to deal with double quote characters in data when quoting strings. The value should be "escape"(the default) or "double". When "escape", the quote character is escaped in C style by a blackslash. When "double", the quote character is doubled.
fileEncoding a character string. The character encoding for the input stream, used for converting input bytes to characters. This can be any of the encodings accepted by iconv, including "native.enc". The default value, the empty string, specifies using options("encoding"). This argument is not used when the file argument is a connection, because a connection already has an associated encoding (see file).
... more arguments to write.table. append, col.names, sep, dec and qmethod cannot be altered.

Details

If col.names = NA and row.names != FALSE, a blank column name is added for the row names. write.csv uses "." as the decimal point and a comma as the separator. write.csv2 uses a comma as the decimal point and a semicolon as the separator.
Side Effects
The file is written.
See Also
cat, read.table, iconv, file.
Examples
data <- data.frame(aa=LETTERS[1:3],bb=101:103,cc=c(NA,2,3))
write.table(data, sep=";")
# Produces the following output:
#   "aa";"bb";"cc"
#   "1";"A";101;NA
#   "2";"B";102;2
#   "3";"C";103;3

write.table(data, quote=FALSE) # Produces the following output: # aa bb cc # 1 A 101 NA # 2 B 102 2 # 3 C 103 3

# SPEC # From R-ex/e.table.R ## 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 SPEC

Package utils version 6.1.4-13
Package Index