conversions | R Documentation |
Convenience functions for converting to different sparse matrix formats, between pairs of classes which might not be supported in the 'Matrix' package.
These come in the form of explicit functions 'as.<type>.matrix' (see below), as well as registered conversion methods to use along with 'as(object, type)', adding extra conversion routes which are missing in the 'Matrix' package for output types 'dgRMatrix', 'lgRMatrix', and 'ngRMatrix'.
as.csr.matrix(x, binary = FALSE, logical = FALSE, sort = FALSE)
as.csc.matrix(x, binary = FALSE, logical = FALSE, sort = FALSE)
as.coo.matrix(x, binary = FALSE, logical = FALSE, sort = FALSE)
as.sparse.vector(x, binary = FALSE, logical = FALSE, integer = FALSE)
x |
A matrix which is to be converted to a different format. Supported input types are:
|
binary |
Whether the result should be a binary-only matrix/vector (inheriting from class ‘nsparseMatrix'/'nsparseVector' - these don’t have slot 'x'). Can only pass one of 'binary' or 'logical'. |
logical |
Whether the result should be a matrix/vector with logical (boolean) type (inheriting from 'lsparseMatrix'/'lsparseVector'). Can only pass one of 'binary' or 'logical'. |
sort |
Whether to sort the indices in case they are not sorted. Note that it will perform deep copies of the indices and values along the way. |
integer |
Whether the result should be a vector with integer type ('isparseVector'). |
The functions internally might use some routes of 'as(x, "?sparseMatrix")', so they might work with other object classes if they register a conversion method for 'Matrix' base types.
When passed a vector, the functions 'as.csr.matrix' and 'as.coo.matrix' will assume that it is a row vector, while ‘as.csc.matrix' will assume it’s a column vector.
A sparse matrix/vector, with format:
CSR (a.k.a. 'RsparseMatrix') when calling 'as.csr.matrix' (class 'dgRMatrix', 'ngRMatrix', or 'lgRMatrix', depending on parameters 'binary' and 'logical').
CSC (a.k.a. 'CsparseMatrix') when calling 'as.csc.matrix' (class 'dgCMatrix', 'ngCMatrix', or 'lgCMatrix', depending on parameters 'binary' and 'logical').
COO (a.k.a. 'TsparseMatrix') when calling 'as.coo.matrix' (class 'dgTMatrix', 'ngTMatrix', or 'lgTMatrix', depending on parameters 'binary' and 'logical').
sparse vector (class dependant on input) when calling 'as.sparse.vector'.
library(Matrix)
library(MatrixExtra)
m.coo <- as(matrix(1:3), "TsparseMatrix")
as.csr.matrix(m.coo)
as.csr.matrix(1:3) # <- assumes it's a row vector
as.csc.matrix(1:3) # <- assumes it's a column vector
### Using the new conversion methods
### (these would fail if 'MatrixExtra' is not loaded)
as(matrix(1:3), "ngRMatrix")
as(as.csc.matrix(m.coo), "dgRMatrix")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.