Skip to contents

Computes data sparsity (proportion of zeros).

Usage

sparsity(x, ...)

# S4 method for class 'matrix'
sparsity(x, count = FALSE)

# S4 method for class 'data.frame'
sparsity(x, count = FALSE)

Arguments

x

An R object (should be a matrix or a data.frame).

...

Currently not used.

count

A logical scalar: should a count be returned instead of a proportion?

Value

A length-one numeric vector.

Details

If x is a data.frame, sparsity is computed on numeric variables only.

See also

Other data summaries: describe()

Author

N. Frerebeau

Examples

## Create a data matrix
X <- matrix(sample(0:9, 15, TRUE), nrow = 3, ncol = 5)

## Add NA
k <- sample(1:15, 3, FALSE)
X[k] <- NA

## Sparsity
sparsity(X)
#> [1] 0.06666667

## Quick description
describe(X)
#> 3 observations, 5 variables:
#> * 5 numeric variables.
#> * 0 categorial variables.
#> * 0 binary variables.
#> 
#> 3 missing values:
#> * 2 observations (67%) contain missing values.
#> * 3 variables (60%) contain missing values.
#> 
#> Data checking:
#> * 7% of numeric values are zero.
#> * 0 variables with no variance.