write_matrix {genio} | R Documentation |
Write a matrix to a file without row or column names
Description
The inverse function of read_matrix()
, this writes what is intended to be a numeric matrix to a tab-delimited file without row or column names present.
It uses readr::write_tsv()
to do it efficiently.
Intended for outputs such as those of admixture inference approaches.
Usage
write_matrix(file, x, ext = "txt", verbose = TRUE, append = FALSE)
Arguments
file |
Output file (whatever is accepted by |
x |
The matrix to write.
Unlike |
ext |
The desired file extension.
If |
verbose |
If |
append |
If |
Value
The output x
, coerced into data.frame, invisibly (what readr::write_tsv()
returns).
See Also
read_matrix()
, the inverse function.
Examples
# to write an existing matrix `x` into file "data.txt", run like this:
# write_matrix( "data", x )
# this also works
# write_matrix( "data.txt", x )
# The following example is more detailed but also more awkward
# because (only for these examples) the package must create the file in a *temporary* location
# create a dummy matrix with the right columns
x <- rbind( 1:3, (0:2)/10, -1:1 )
# a dummy file
file_out <- tempfile('delete-me-example', fileext = '.txt') # will also work without extension
# write the matrix without header
write_matrix( file_out, x )
# delete output when done
file.remove( file_out )