Learn R Programming

recommenderlab (version 0.2-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)

Arguments

x

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

Value

Returns a dgCMatrix or a matrix, respectively.

Details

The representation is based on the sparse dgCMatrix in Matrix but instead of zeros, NAs are dropped.

Note: Be careful when working with matrix operations (multiplication, addition, etc.) since these will use the dgCMatrix superclass which assumes that all dropped values are zero and not NA! This means that the operations might remove zeros or add to NAs incorrectly.

dropNAis.na correctly finds NA values in a sparse matrix with dropped NA values, while is.na() is not able to distinguish between 0s and NAs.

See Also

dgCMatrix in Matrix.

Examples

Run this code
# NOT RUN {
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
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