Returns the number of non-zero values of a numeric-like R object, and
in particular an object
x inheriting from class
an R object, typically inheriting from class
For sparse matrices, you may often want to use
the number of non zero entries in
Note that for a symmetric sparse matrix
S (i.e., inheriting from
typically twice the
signature(x = "ANY")
the default method for
Matrix class objects, simply counts the
NA's depending on
na.counted argument, see above.
signature(x = "denseMatrix")
conceptually the same as
matrix objects, care has to be taken
signature(x = "diagonalMatrix"), and
signature(x = "indMatrix")
fast simple methods for these
signature(x = "sparseMatrix")
typically, the most
interesting method, also carefully taking
"symmetricMatrix" objects into account.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
m <- Matrix(0+1:28, nrow = 4) m[-3,c(2,4:5,7)] <- m[ 3, 1:4] <- m[1:3, 6] <- 0 (mT <- as(m, "dgTMatrix")) nnzero(mT) (S <- crossprod(mT)) nnzero(S) str(S) # slots are smaller than nnzero() stopifnot(nnzero(S) == sum(as.matrix(S) != 0))# failed earlier data(KNex) M <- KNex$mm class(M) dim(M) length(M); stopifnot(length(M) == prod(dim(M))) nnzero(M) # more relevant than length ## the above are also visible from str(M)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.