Skip to content

Instantly share code, notes, and snippets.

@slowkow
Created March 16, 2020 21:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slowkow/30410195568924b05a624a9acab58313 to your computer and use it in GitHub Desktop.
Save slowkow/30410195568924b05a624a9acab58313 to your computer and use it in GitHub Desktop.
Write a gzip compressed Matrix Market file in R.
#' @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