Learn R Programming

recommenderlab (version 0.1-4)

dropNA: 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)

Arguments

x
a matrix (for dropNA()) or a dgCMatrix (for dropNA2matrix())

Value

  • Returns a dgCMatrix or a matrix.

Details

The representation is based on dgCMatrix in Matrix but instead of zeros, NAs are dropped. Be careful when working with the dgCMatrix directly since all dropped values are NA and not 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='')))

## drop all NAs in the representation
sparse <- dropNA(m)
sparse

## convert back to matrix
dropNA2matrix(sparse)

## Using regular coercion to dgCMatrix. Note that regular coercion 
## to dgCMatrix drops 0s and not NAs!
as(m, "dgCMatrix")

Run the code above in your browser using DataLab