Format and Print Sparse Matrices Flexibly
printSpMatrix.RdFormat and print sparse matrices flexibly. These are the “workhorses” used by
the format, show and print
methods for sparse matrices. If x is large,
printSpMatrix2(x) calls printSpMatrix() twice, namely,
for the first and the last few rows, suppressing those in between, and
also suppresses columns when x is too wide.
printSpMatrix() basically prints the result of
formatSpMatrix().
Usage
formatSpMatrix(x, digits = NULL, maxp = 1e9,
cld = getClassDef(class(x)), zero.print = ".",
col.names, note.dropping.colnames = TRUE, uniDiag = TRUE,
align = c("fancy", "right"), ...)
printSpMatrix(x, digits = NULL, maxp = max(100L, getOption("max.print")),
cld = getClassDef(class(x)),
zero.print = ".", col.names, note.dropping.colnames = TRUE,
uniDiag = TRUE, col.trailer = "",
align = c("fancy", "right"), ...)
printSpMatrix2(x, digits = NULL, maxp = max(100L, getOption("max.print")),
zero.print = ".", col.names, note.dropping.colnames = TRUE,
uniDiag = TRUE, suppRows = NULL, suppCols = NULL,
col.trailer = if(suppCols) "......" else "",
align = c("fancy", "right"),
width = getOption("width"), fitWidth = TRUE, ...)Arguments
- x
an R object inheriting from class
sparseMatrix.- digits
significant digits to use for printing, see
print.default, the default,NULL, corresponds to usinggetOption("digits").- maxp
integer, default from
options(max.print), influences how many entries of large matrices are printed at all. Typically should not be smaller than around 1000; values smaller than 100 are silently “rounded up” to 100.
- cld
the class definition of
x; must be equivalent togetClassDef(class(x))and exists mainly for possible speedup.- zero.print
character which should be printed for structural zeroes. The default
"."may occasionally be replaced by" "(blank); using"0"would look almost likeprint()ing of non-sparse matrices.- col.names
logical or string specifying if and how column names of
xshould be printed, possibly abbreviated. The default is taken fromoptions("sparse.colnames")if that is set, otherwiseFALSEunless there are less than ten columns. WhenTRUEthe full column names are printed.
Whencol.namesis a string beginning with"abb"or"sub"and ending with an integern(i.e., of the form"abb... <n>"), the column names areabbreviate()d orsubstring()ed to (target) lengthn, see the examples.- note.dropping.colnames
logical specifying, when
col.namesisFALSEif the dropping of the column names should be noted,TRUEby default.- 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”).- col.trailer
a string to be appended to the right of each column; this is typically made use of by
show(<sparseMatrix>)only, when suppressing columns.- suppRows, suppCols
logicals or
NULL, forprintSpMatrix2()specifying if rows or columns should be suppressed in printing. IfNULL, sensible defaults are determined fromdim(x)andoptions(c("width", "max.print")). Setting both toFALSEmay be a very bad idea.- align
a string specifying how the
zero.printcodes should be aligned, i.e., padded as strings. The default,"fancy", takes some effort to align the typicalzero.print = "."with the position of0, i.e., the first decimal (one left of decimal point) of the numbers printed, whereasalign = "right"just makes use ofprint(*, right = TRUE).- width
number, a positive integer, indicating the approximately desired (line) width of the output, see also
fitWidth.- fitWidth
logical indicating if some effort should be made to match the desired
widthor temporarily enlarge that if deemed necessary.- ...
unused optional arguments.
Details
- formatSpMatrix:
If
xis large, only the first rows making up the approximately firstmaxpentries is used, otherwise all ofx..formatSparseSimple()is applied to (a dense version of) the matrix. Then,formatSparseMis used, unless in trivial cases or for sparse matrices withoutxslot.
Value
- formatSpMatrix()
returns a character matrix with possibly empty column names, depending on
col.namesetc, see above.- printSpMatrix*()
return
xinvisibly, seeinvisible.
See also
the virtual class sparseMatrix and the
classes extending it; maybe sparseMatrix or
spMatrix as simple constructors of such matrices.
The underlying utilities formatSparseM and
.formatSparseSimple() (on the same page).
Examples
f1 <- gl(5, 3, labels = LETTERS[1:5])
X <- as(f1, "sparseMatrix")
X ## <==> show(X) <==> print(X)
#> 5 x 15 sparse Matrix of class "dgCMatrix"
#>
#> A 1 1 1 . . . . . . . . . . . .
#> B . . . 1 1 1 . . . . . . . . .
#> C . . . . . . 1 1 1 . . . . . .
#> D . . . . . . . . . 1 1 1 . . .
#> E . . . . . . . . . . . . 1 1 1
t(X) ## shows column names, since only 5 columns
#> 15 x 5 sparse Matrix of class "dgCMatrix"
#> A B C D E
#> [1,] 1 . . . .
#> [2,] 1 . . . .
#> [3,] 1 . . . .
#> [4,] . 1 . . .
#> [5,] . 1 . . .
#> [6,] . 1 . . .
#> [7,] . . 1 . .
#> [8,] . . 1 . .
#> [9,] . . 1 . .
#> [10,] . . . 1 .
#> [11,] . . . 1 .
#> [12,] . . . 1 .
#> [13,] . . . . 1
#> [14,] . . . . 1
#> [15,] . . . . 1
X2 <- as(gl(12, 3, labels = paste(LETTERS[1:12],"c",sep=".")),
"sparseMatrix")
X2
#> 12 x 36 sparse Matrix of class "dgCMatrix"
#>
#> A.c 1 1 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#> B.c . . . 1 1 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#> C.c . . . . . . 1 1 1 . . . . . . . . . . . . . . . . . . . . . . . . . . .
#> D.c . . . . . . . . . 1 1 1 . . . . . . . . . . . . . . . . . . . . . . . .
#> E.c . . . . . . . . . . . . 1 1 1 . . . . . . . . . . . . . . . . . . . . .
#> F.c . . . . . . . . . . . . . . . 1 1 1 . . . . . . . . . . . . . . . . . .
#> G.c . . . . . . . . . . . . . . . . . . 1 1 1 . . . . . . . . . . . . . . .
#> H.c . . . . . . . . . . . . . . . . . . . . . 1 1 1 . . . . . . . . . . . .
#> I.c . . . . . . . . . . . . . . . . . . . . . . . . 1 1 1 . . . . . . . . .
#> J.c . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1 1 . . . . . .
#> K.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1 1 . . .
#> L.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1 1
## less nice, but possible:
print(X2, col.names = TRUE) # use [,1] [,2] .. => does not fit
#> 12 x 36 sparse Matrix of class "dgCMatrix"
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
#> A.c 1 1 1 . . . . . . . . . . .
#> B.c . . . 1 1 1 . . . . . . . .
#> C.c . . . . . . 1 1 1 . . . . .
#> D.c . . . . . . . . . 1 1 1 . .
#> E.c . . . . . . . . . . . . 1 1
#> F.c . . . . . . . . . . . . . .
#> G.c . . . . . . . . . . . . . .
#> H.c . . . . . . . . . . . . . .
#> I.c . . . . . . . . . . . . . .
#> J.c . . . . . . . . . . . . . .
#> K.c . . . . . . . . . . . . . .
#> L.c . . . . . . . . . . . . . .
#> [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25] [,26]
#> A.c . . . . . . . . . . . .
#> B.c . . . . . . . . . . . .
#> C.c . . . . . . . . . . . .
#> D.c . . . . . . . . . . . .
#> E.c 1 . . . . . . . . . . .
#> F.c . 1 1 1 . . . . . . . .
#> G.c . . . . 1 1 1 . . . . .
#> H.c . . . . . . . 1 1 1 . .
#> I.c . . . . . . . . . . 1 1
#> J.c . . . . . . . . . . . .
#> K.c . . . . . . . . . . . .
#> L.c . . . . . . . . . . . .
#> [,27] [,28] [,29] [,30] [,31] [,32] [,33] [,34] [,35] [,36]
#> A.c . . . . . . . . . .
#> B.c . . . . . . . . . .
#> C.c . . . . . . . . . .
#> D.c . . . . . . . . . .
#> E.c . . . . . . . . . .
#> F.c . . . . . . . . . .
#> G.c . . . . . . . . . .
#> H.c . . . . . . . . . .
#> I.c 1 . . . . . . . . .
#> J.c . 1 1 1 . . . . . .
#> K.c . . . . 1 1 1 . . .
#> L.c . . . . . . . 1 1 1
## Possibilities with column names printing:
t(X2) # suppressing column names
#> 36 x 12 sparse Matrix of class "dgCMatrix"
#> [[ suppressing 12 column names ‘A.c’, ‘B.c’, ‘C.c’ ... ]]
#>
#> [1,] 1 . . . . . . . . . . .
#> [2,] 1 . . . . . . . . . . .
#> [3,] 1 . . . . . . . . . . .
#> [4,] . 1 . . . . . . . . . .
#> [5,] . 1 . . . . . . . . . .
#> [6,] . 1 . . . . . . . . . .
#> [7,] . . 1 . . . . . . . . .
#> [8,] . . 1 . . . . . . . . .
#> [9,] . . 1 . . . . . . . . .
#> [10,] . . . 1 . . . . . . . .
#> [11,] . . . 1 . . . . . . . .
#> [12,] . . . 1 . . . . . . . .
#> [13,] . . . . 1 . . . . . . .
#> [14,] . . . . 1 . . . . . . .
#> [15,] . . . . 1 . . . . . . .
#> [16,] . . . . . 1 . . . . . .
#> [17,] . . . . . 1 . . . . . .
#> [18,] . . . . . 1 . . . . . .
#> [19,] . . . . . . 1 . . . . .
#> [20,] . . . . . . 1 . . . . .
#> [21,] . . . . . . 1 . . . . .
#> [22,] . . . . . . . 1 . . . .
#> [23,] . . . . . . . 1 . . . .
#> [24,] . . . . . . . 1 . . . .
#> [25,] . . . . . . . . 1 . . .
#> [26,] . . . . . . . . 1 . . .
#> [27,] . . . . . . . . 1 . . .
#> [28,] . . . . . . . . . 1 . .
#> [29,] . . . . . . . . . 1 . .
#> [30,] . . . . . . . . . 1 . .
#> [31,] . . . . . . . . . . 1 .
#> [32,] . . . . . . . . . . 1 .
#> [33,] . . . . . . . . . . 1 .
#> [34,] . . . . . . . . . . . 1
#> [35,] . . . . . . . . . . . 1
#> [36,] . . . . . . . . . . . 1
print(t(X2), col.names=TRUE)
#> 36 x 12 sparse Matrix of class "dgCMatrix"
#> A.c B.c C.c D.c E.c F.c G.c H.c I.c J.c K.c L.c
#> [1,] 1 . . . . . . . . . . .
#> [2,] 1 . . . . . . . . . . .
#> [3,] 1 . . . . . . . . . . .
#> [4,] . 1 . . . . . . . . . .
#> [5,] . 1 . . . . . . . . . .
#> [6,] . 1 . . . . . . . . . .
#> [7,] . . 1 . . . . . . . . .
#> [8,] . . 1 . . . . . . . . .
#> [9,] . . 1 . . . . . . . . .
#> [10,] . . . 1 . . . . . . . .
#> [11,] . . . 1 . . . . . . . .
#> [12,] . . . 1 . . . . . . . .
#> [13,] . . . . 1 . . . . . . .
#> [14,] . . . . 1 . . . . . . .
#> [15,] . . . . 1 . . . . . . .
#> [16,] . . . . . 1 . . . . . .
#> [17,] . . . . . 1 . . . . . .
#> [18,] . . . . . 1 . . . . . .
#> [19,] . . . . . . 1 . . . . .
#> [20,] . . . . . . 1 . . . . .
#> [21,] . . . . . . 1 . . . . .
#> [22,] . . . . . . . 1 . . . .
#> [23,] . . . . . . . 1 . . . .
#> [24,] . . . . . . . 1 . . . .
#> [25,] . . . . . . . . 1 . . .
#> [26,] . . . . . . . . 1 . . .
#> [27,] . . . . . . . . 1 . . .
#> [28,] . . . . . . . . . 1 . .
#> [29,] . . . . . . . . . 1 . .
#> [30,] . . . . . . . . . 1 . .
#> [31,] . . . . . . . . . . 1 .
#> [32,] . . . . . . . . . . 1 .
#> [33,] . . . . . . . . . . 1 .
#> [34,] . . . . . . . . . . . 1
#> [35,] . . . . . . . . . . . 1
#> [36,] . . . . . . . . . . . 1
print(t(X2), zero.print = "", col.names="abbr. 1")
#> 36 x 12 sparse Matrix of class "dgCMatrix"
#> A B C D E F G H I J K L
#> [1,] 1
#> [2,] 1
#> [3,] 1
#> [4,] 1
#> [5,] 1
#> [6,] 1
#> [7,] 1
#> [8,] 1
#> [9,] 1
#> [10,] 1
#> [11,] 1
#> [12,] 1
#> [13,] 1
#> [14,] 1
#> [15,] 1
#> [16,] 1
#> [17,] 1
#> [18,] 1
#> [19,] 1
#> [20,] 1
#> [21,] 1
#> [22,] 1
#> [23,] 1
#> [24,] 1
#> [25,] 1
#> [26,] 1
#> [27,] 1
#> [28,] 1
#> [29,] 1
#> [30,] 1
#> [31,] 1
#> [32,] 1
#> [33,] 1
#> [34,] 1
#> [35,] 1
#> [36,] 1
print(t(X2), zero.print = "-", col.names="substring 2")
#> 36 x 12 sparse Matrix of class "dgCMatrix"
#> A. B. C. D. E. F. G. H. I. J. K. L.
#> [1,] 1 - - - - - - - - - - -
#> [2,] 1 - - - - - - - - - - -
#> [3,] 1 - - - - - - - - - - -
#> [4,] - 1 - - - - - - - - - -
#> [5,] - 1 - - - - - - - - - -
#> [6,] - 1 - - - - - - - - - -
#> [7,] - - 1 - - - - - - - - -
#> [8,] - - 1 - - - - - - - - -
#> [9,] - - 1 - - - - - - - - -
#> [10,] - - - 1 - - - - - - - -
#> [11,] - - - 1 - - - - - - - -
#> [12,] - - - 1 - - - - - - - -
#> [13,] - - - - 1 - - - - - - -
#> [14,] - - - - 1 - - - - - - -
#> [15,] - - - - 1 - - - - - - -
#> [16,] - - - - - 1 - - - - - -
#> [17,] - - - - - 1 - - - - - -
#> [18,] - - - - - 1 - - - - - -
#> [19,] - - - - - - 1 - - - - -
#> [20,] - - - - - - 1 - - - - -
#> [21,] - - - - - - 1 - - - - -
#> [22,] - - - - - - - 1 - - - -
#> [23,] - - - - - - - 1 - - - -
#> [24,] - - - - - - - 1 - - - -
#> [25,] - - - - - - - - 1 - - -
#> [26,] - - - - - - - - 1 - - -
#> [27,] - - - - - - - - 1 - - -
#> [28,] - - - - - - - - - 1 - -
#> [29,] - - - - - - - - - 1 - -
#> [30,] - - - - - - - - - 1 - -
#> [31,] - - - - - - - - - - 1 -
#> [32,] - - - - - - - - - - 1 -
#> [33,] - - - - - - - - - - 1 -
#> [34,] - - - - - - - - - - - 1
#> [35,] - - - - - - - - - - - 1
#> [36,] - - - - - - - - - - - 1
#> [1] "/tmp/RtmprkKtLd/file496a72c4ab9d"