Learn R Programming

sparsevctrs (version 0.3.3)

sparsity: Calculate sparsity of data frames, matrices, and sparse matrices

Description

Turning data frame with sparse columns into sparse matrix using Matrix::sparseMatrix().

Usage

sparsity(x, sample = NULL)

Value

a single number, between 0 and 1.

Arguments

x

a data frame, matrix of sparse matrix.

sample

a integer or NULL. Number of rows to sample to estimate sparsity. If NULL then no sampling is performed. Will not be used when x is a sparse matrix. Defaults to NULL.

Details

Only numeric 0s are considered zeroes in this calculations. Missing values, logical vectors and then string "0" aren't counted.

Examples

Run this code

# data frame
sparsity(mtcars)

# Matrix
set.seed(1234)
mat <- matrix(sample(0:1, 100, TRUE, c(0.9, 0.1)), nrow = 10)
colnames(mat) <- letters[1:10]

sparsity(mat)

# Sparse matrix
sparse_mat <- Matrix::Matrix(mat, sparse = TRUE)

sparsity(sparse_mat)

Run the code above in your browser using DataLab