is_sparse: Check for sparse representation

is_sparseR Documentation

Check for sparse representation

Description

is_sparse indicates whether an object (typically array-like) uses a sparse representation of the data or not.

Note that this is about data representation and not about the data itself. For example, is_sparse() always returns FALSE on an ordinary matrix, even if the matrix contains 99% zeros, because the data in such a matrix is always stored in a dense form. OTOH is_sparse() always returns TRUE on a SparseArray derivative from the SparseArray package, or on a dgCMatrix object from the Matrix package, even if the data contains no zeros, because these objects use a sparse representation of the data.

Usage

is_sparse(x)

Arguments

x

Any object, but will typically be an array-like object.

Examples of array-like objects: ordinary arrays, SparseArray objects from the SparseArray package, dgCMatrix objects from the Matrix package, DelayedArray objects from the DelayedArray package, or any object with an array semantic (i.e. an object for which dim(x) is not NULL).

Value

TRUE or FALSE

See Also

  • read_block to read a block of data from an array-like object.

  • array and matrix objects in base R.

  • dgCMatrix objects implemented in the Matrix package.

Examples

m <- matrix(0L, nrow=50, ncol=20)
stopifnot(identical(is_sparse(m), FALSE))

dgc <- as(m + runif(1000), "dgCMatrix")
stopifnot(identical(is_sparse(dgc), TRUE))

Bioconductor/S4Arrays documentation built on Oct. 30, 2024, 12:13 p.m.