sparseNAMatrix: Sparse Matrix Representation With NAs Not Explicitly Stored

sparseNAMatrix-classR Documentation

Sparse Matrix Representation With NAs Not Explicitly Stored


Coerce from and to a sparse matrix representation where NAs are not explicitly stored.





a matrix for dropNA(), or a sparse matrix with dropped NA values for dropNA2matrix() or


The representation is based on the sparse dgCMatrix in Matrix but instead of zeros, NAs are dropped. This is achieved by the following:

  • Zeros are represented with a very small value (.Machine$double.xmin) so they do not get dropped in the sparse representation.

  • NAs are converted to 0 before cercions to dgCMatrix to make them not explicitly stored.

Caution: Be careful when working with the sparse matrix and sparse matrix operations (multiplication, addition, etc.) directly.

  • Sparse matrix operations will see 0 where NAs should be.

  • Actual zero ratings have a small, but non-zero value (.Machine$double.xmin).

  • Sparse matrix operations that can result in a true 0 need to be followed by replacing the 0 with .Machine$double.xmin or other operations (like subsetting) may drop the 0. correctly finds NA values in a sparse matrix with dropped NA values, while does not work.

dropNA2matrix() converts the sparse representation into a dense matrix. NAs represented by dropped values are converted to true NAs. Zeros are recovered by using zapsmall() which replaces small values by 0.


Returns a dgCMatrix or a matrix, respectively.

See Also

dgCMatrix in Matrix.


m <- matrix(sample(c(NA,0:5),50, replace=TRUE, prob=c(.5,rep(.5/6,6))),
    nrow=5, ncol=10, dimnames = list(users=paste('u', 1:5, sep=''),
    items=paste('i', 1:10, sep='')))

## drop all NAs in the representation. Zeros are represented by very small values.
sparse <- dropNA(m)

## convert back to matrix

## Note: be careful with the sparse representation!
## Do not use, but use

recommenderlab documentation built on Aug. 18, 2022, 1:06 a.m.