Learn R Programming

recommenderlab (version 1.0.6)

sparseNAMatrix-class: Sparse Matrix Representation With NAs Not Explicitly Stored

Description

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

Usage

dropNA(x)
dropNA2matrix(x)
dropNAis.na(x)

Value

Returns a dgCMatrix or a matrix, respectively.

Arguments

x

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

Details

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.

dropNAis.na() correctly finds NA values in a sparse matrix with dropped NA values, while is.na() 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.

See Also

dgCMatrix in Matrix.

Examples

Run this code
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='')))
m

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

## convert back to matrix
dropNA2matrix(sparse)

## Note: be careful with the sparse representation!
## Do not use is.na, but use
dropNAis.na(sparse)

Run the code above in your browser using DataLab