Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

sparsity

Sparsity


Description

Show the sparsity (as a count or proportion) of a matrix. For example, .99 sparsity means 99% of the values are zero. Similarly, a sparsity of 0 means the matrix is fully dense.

Usage

sparsity(x, proportion = TRUE)

Arguments

x

The matrix, stored as an ordinary R matrix or as a "simple triplet matrix" (from the slam package).

proportion

Logical; should a proportion or a count be returned?

Details

The implementation is very efficient for dense matrices. For sparse triplet matrices, the count is trivial.

Value

The sparsity of the input matrix, as a proportion or a count.

Author(s)

Drew Schmidt

Examples

## Completely sparse matrix
x <- matrix(0, 10, 10)
coop::sparsity(x)

## 15\% density / 85\% sparsity
x[sample(length(x), size=15)] <- 1
coop::sparsity(x)

coop

Co-Operation: Fast Covariance, Correlation, and Cosine Similarity Operations

v0.6-2
BSD 2-clause License + file LICENSE
Authors
Drew Schmidt [aut, cre], Christian Heckendorf [ctb] (Caught some memory errors.)
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.