sparseNAMatrix-class | R Documentation |
Coerce from and to a
sparse matrix representation where NA
s are not explicitly stored.
dropNA(x) dropNA2matrix(x) dropNAis.na(x)
x |
a matrix for |
The representation is based on
the sparse dgCMatrix
in Matrix but instead of zeros, NA
s 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.
Returns a dgCMatrix or a matrix, respectively.
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=''))) 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.