Formatting Sparse Numeric Matrices Utilities
formatSparseM.RdUtilities for formatting sparse numeric matrices in a flexible way.
These functions are used by the format and print
methods for sparse matrices and can be applied as well to standard R
matrices. Note that all arguments but the first are optional.
formatSparseM() is the main “workhorse” of
formatSpMatrix, the format method for sparse
matrices.
.formatSparseSimple() is a simple helper function, also dealing
with (short/empty) column names construction.
Arguments
- x
an R object inheriting from class
sparseMatrix.- zero.print
character which should be used for structural zeroes. The default
"."may occasionally be replaced by" "(blank); using"0"would look almost likeprint()ing of non-sparse matrices.- align
a string specifying how the
zero.printcodes should be aligned, seeformatSpMatrix.- m
(optional) a (standard R)
matrixversion ofx.- asLogical
should the matrix be formatted as a logical matrix (or rather as a numeric one); mostly for
formatSparseM().- uniDiag
logical indicating if the diagonal entries of a sparse unit triangular or unit-diagonal matrix should be formatted as
"I"instead of"1"(to emphasize that the 1's are “structural”).- digits
significant digits to use for printing, see
print.default.- cx
(optional) character matrix; a formatted version of
x, still with strings such as"0.00"for the zeros.- iN0
(optional) integer vector, specifying the location of the non-zeroes of
x.- col.names, note.dropping.colnames
see
formatSpMatrix.- dn
dimnamesto be used; a list (of length two) with row and column names (orNULL).
See also
formatSpMatrix which calls formatSparseM() and is
the format method for sparse matrices.printSpMatrix which is used by the (typically
implicitly called) show and print methods
for sparse matrices.
Value
a character matrix like cx, where the zeros have been replaced
with (padded versions of) zero.print.
As this is a dense matrix, do not use these functions for
really large (really) sparse matrices!
Examples
m <- suppressWarnings(matrix(c(0, 3.2, 0,0, 11,0,0,0,0,-7,0), 4,9))
fm <- formatSparseM(m)
noquote(fm)
#>
#> [1,] . 11 . 3.2 . -7.0 . . .
#> [2,] 3.2 . -7 . . . . . .
#> [3,] . . . . . . 11 . 3.2
#> [4,] . . . 11.0 . 3.2 . -7 .
## nice, but this is nicer {with "units" vertically aligned}:
print(fm, quote=FALSE, right=TRUE)
#>
#> [1,] . 11 . 3.2 . -7.0 . . .
#> [2,] 3.2 . -7 . . . . . .
#> [3,] . . . . . . 11 . 3.2
#> [4,] . . . 11.0 . 3.2 . -7 .
## and "the same" as :
Matrix(m)
#> 4 x 9 sparse Matrix of class "dgCMatrix"
#>
#> [1,] . 11 . 3.2 . -7.0 . . .
#> [2,] 3.2 . -7 . . . . . .
#> [3,] . . . . . . 11 . 3.2
#> [4,] . . . 11.0 . 3.2 . -7 .
## align = "right" is cheaper --> the "." are not aligned:
noquote(f2 <- formatSparseM(m,align="r"))
#>
#> [1,] . 11 . 3.2 . -7.0 . . .
#> [2,] 3.2 . -7 . . . . . .
#> [3,] . . . . . . 11 . 3.2
#> [4,] . . . 11.0 . 3.2 . -7 .
stopifnot(f2 == fm | m == 0, dim(f2) == dim(m),
(f2 == ".") == (m == 0))