View source: R/sparse_matrix_functions.R
col_nnz | R Documentation |
Find the number of non-zero elements in a matrix per row, per column or in total. In the case of sparse matrices (class dgTMatrix
, dgCMatrix
or dgRMatrix
), this is much faster than using apply
and/or sum
. Note there already exists a function nnzero
in the Matrix package that finds the number of non-zero elements in a sparse matrix, but nnz
is faster.
col_nnz(mat)
row_nnz(mat)
nnz(mat)
mat |
A matrix. May be sparse (i.e. of class |
An integer or named integer vector.
mat <- matrix(rbinom(900, 1, 0.1), 30, 30)
col_nnz(mat)
rownames(mat) <- replicate(30, paste(sample(letters, 10), collapse = ''))
row_nnz(mat)
nnz(mat)
mat <- as(mat, 'dgCMatrix')
row_nnz(mat)
nnz(mat)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.