| sparseMatrix-class {Matrix} | R Documentation |
Virtual Class "sparseMatrix" — Mother of Sparse Matrices
Description
Virtual Mother Class of All Sparse Matrices
Slots
Dim:Object of class
"integer"- the dimensions of the matrix - must be an integer vector with exactly two non-negative values.Dimnames:a list of length two - inherited from class
Matrix, seeMatrix.
Extends
Class "Matrix", directly.
Methods
- show
(object = "sparseMatrix"): Theshowmethod for sparse matrices prints “structural” zeroes as"."usingprintSpMatrix()which allows further customization.signature(x = "sparseMatrix"), ....
Theprintmethod for sparse matrices by default is the same asshow()but can be called with extra optional arguments, seeprintSpMatrix().- format
signature(x = "sparseMatrix"), ....
Theformatmethod for sparse matrices, seeformatSpMatrix()for details such as the extra optional arguments.- summary
(object = "sparseMatrix", uniqT=FALSE): Returns an object of S3 class"sparseSummary"which is basically adata.framewith columns(i,j,x)(or just(i,j)fornsparseMatrixclass objects) with the stored (typically non-zero) entries. Theprintmethod resembles Matlab's way of printing sparse matrices, and also the MatrixMarket format, seewriteMM.- cbind2
(x = *, y = *): several methods for binding matrices together, column-wise, see the basiccbindandrbindfunctions.
Note that the result will typically be sparse, even when one argument is dense and larger than the sparse one.- rbind2
(x = *, y = *): binding matrices together row-wise, seecbind2above.- determinant
(x = "sparseMatrix", logarithm=TRUE):determinant()methods for sparse matrices typically work viaCholeskyorludecompositions.- diag
(x = "sparseMatrix"): extracts the diagonal of a sparse matrix.- dim<-
signature(x = "sparseMatrix", value = "ANY"): allows to reshape a sparse matrix to a sparse matrix with the same entries but different dimensions.valuemust be of length two and fulfillprod(value) == prod(dim(x)).- coerce
signature(from = "factor", to = "sparseMatrix"): Coercion of a factor to"sparseMatrix"produces the matrix of indicator rows stored as an object of class"dgCMatrix". To obtain columns representing the interaction of the factor and a numeric covariate, replace the"x"slot of the result by the numeric covariate then take the transpose. Missing values (NA) from the factor are translated to columns of all0s.
See also colSums, norm,
...
for methods with separate help pages.
Note
In method selection for multiplication operations (i.e. %*%
and the two-argument form of crossprod)
the sparseMatrix class takes precedence in the sense that if one
operand is a sparse matrix and the other is any type of dense matrix
then the dense matrix is coerced to a dgeMatrix and the
appropriate sparse matrix method is used.
See Also
sparseMatrix, and its references, such as
xtabs(*, sparse=TRUE), or
sparse.model.matrix(),
for constructing sparse matrices.
T2graph for conversion of "graph" objects
(package graph) to and from sparse matrices.
Examples
showClass("sparseMatrix") ## and look at the help() of its subclasses
M <- Matrix(0, 10000, 100)
M[1,1] <- M[2,3] <- 3.14
M ## show(.) method suppresses printing of the majority of rows
data(CAex, package = "Matrix")
dim(CAex) # 72 x 72 matrix
determinant(CAex) # works via sparse lu(.)
## factor -> t( <sparse design matrix> ) :
(fact <- gl(5, 3, 30, labels = LETTERS[1:5]))
(Xt <- as(fact, "sparseMatrix")) # indicator rows
## missing values --> all-0 columns:
f.mis <- fact
i.mis <- c(3:5, 17)
is.na(f.mis) <- i.mis
Xt != (X. <- as(f.mis, "sparseMatrix")) # differ only in columns 3:5,17
stopifnot(all(X.[,i.mis] == 0), all(Xt[,-i.mis] == X.[,-i.mis]))