Write Data to a File
The data (usually a matrix)
x are written to file
x is a two-dimensional matrix you need to transpose it to get the
file the same as those in the internal representation.
write(x, file = "data", ncolumns = if(is.character(x)) 1 else 5, append = FALSE, sep = " ")
- the data to be written out, usually an atomic vector.
- A connection, or a character string naming the file to write to.
"", print to the standard output connection. unix If it is
"|cmd", the output is piped to the command given by cmd.
- the number of columns to write the data in.
xare appended to the connection.
- a string used to separate columns. Using
sep = "\t"gives tab delimited output; default is
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
write is a wrapper for
cat, which gives further
details on the format used.
# create a 2 by 5 matrix x <- matrix(1:10, ncol = 5) # the file data contains x, two rows, five cols # 1 3 5 7 9 will form the first row write(t(x)) # Writing to the "console" 'tab-delimited' # two rows, five cols but the first row is 1 2 3 4 5 write(x, "", sep = "\t") unlink("data") # tidy up