| write {base} | R Documentation |
Write Data to a File
Description
Write data x to a file or other connection.
As it simply calls cat(), less formatting happens than
with print()ing.
If x is a matrix you need to transpose it (and typically set
ncolumns) to get the columns in file the same as those in
the internal representation.
Whereas atomic vectors (numeric, character,
etc, including matrices) are written plainly, i.e., without any names,
less simple vector-like objects such as "factor",
"Date", or "POSIXt" may be
formatted to character before writing.
Usage
write(x, file = "data",
ncolumns = if(is.character(x)) 1 else 5,
append = FALSE, sep = " ")
Arguments
x |
the data to be written out. |
file |
a When |
ncolumns |
the number of columns to write the data in. |
append |
if |
sep |
a string used to separate columns. Using |
References
Becker RA, Chambers JM, Wilks AR (1988). The New S Language. Chapman and Hall/CRC, London.
See Also
write is a wrapper for cat, which gives further
details on the format used.
write.table for matrix and data frame objects,
writeLines for lines of text,
and scan for reading data.
saveRDS and save are often preferable (for
writing any R objects).
Examples
# Demonstrate default ncolumns, writing to the console
write(month.abb, "") # 1 element per line for "character"
write(stack.loss, "") # 5 elements per line for "numeric"
# Build a file with sequential calls
fil <- tempfile("data")
write("# Model settings", fil)
write(month.abb, fil, ncolumns = 6, append = TRUE)
write("\n# Initial parameter values", fil, append = TRUE)
write(sqrt(stack.loss), fil, append = TRUE)
if(interactive()) file.show(fil)
unlink(fil) # tidy up