Created
March 16, 2020 21:08
-
-
Save slowkow/30410195568924b05a624a9acab58313 to your computer and use it in GitHub Desktop.
Write a gzip compressed Matrix Market file in R.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' @param x A sparse matrix from the Matrix package. | |
#' @param file A filename that ends in ".gz". | |
writeMMgz <- function(x, file) { | |
mtype <- "real" | |
if (is(x, "ngCMatrix")) { | |
mtype <- "integer" | |
} | |
writeLines( | |
c( | |
sprintf("%%%%MatrixMarket matrix coordinate %s general", mtype), | |
sprintf("%s %s %s", x@Dim[1], x@Dim[2], length(x@x)) | |
), | |
gzfile(file) | |
) | |
data.table::fwrite( | |
x = summary(x), | |
file = file, | |
append = TRUE, | |
sep = " ", | |
row.names = FALSE, | |
col.names = FALSE | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment