Rev 4344 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
\name{nnzero}\alias{nnzero}\title{The Number of Non-Zero Values of a Matrix}\description{Returns the number of non-zero values of a numeric-like \R object, andin particular an object \code{x} inheriting from class\code{\linkS4class{Matrix}}.}\usage{nnzero(x, na.counted = NA)}\arguments{\item{x}{an \R object, typically inheriting from class\code{\linkS4class{Matrix}} or \code{\link{numeric}}.}\item{na.counted}{a \code{\link{logical}} describing how\code{\link{NA}}s should be counted. There are three possiblesettings for \code{na.counted}:\describe{\item{TRUE}{\code{NA}s \emph{are} counted as non-zero (since\dQuote{they are not zero}).}\item{NA}{the result will be \code{NA} if there are \code{NA}'s in\code{x} (since \dQuote{NA's are not known, i.e. Not Available}).}\item{FALSE}{\code{NA}s are \emph{omitted} from \code{x} beforethe non-zero entries are counted.}}}}% \details{% }\value{the number of non zero entries in \code{x} (typically\code{\link{integer}}).Note that for a \emph{symmetric} sparse matrix \code{S} (i.e., inheriting fromclass \code{\linkS4class{symmetricMatrix}}), \code{nnzero(S)} istypically \emph{twice} the \code{length(S@x)}.}%\author{Martin}\seealso{The \code{\linkS4class{Matrix}} class also has a\code{\link{length}} method; typically, \code{length(M)} is muchlarger than \code{nnzero(M)} for a sparse matrix M, and the latter isa better indication of the \emph{size} of \code{M}.}\examples{m <- Matrix(0+1:28, nrow = 4)m[-3,c(2,4:5,7)] <- m[ 3, 1:4] <- m[1:3, 6] <- 0(mT <- as(m, "dgTMatrix"))nnzero(mT)(S <- crossprod(mT))nnzero(S)str(S) # slots are smaller than nnzero()stopifnot(nnzero(S) == sum(as.matrix(S) != 0))# failed earlierdata(KNex)M <- KNex$mmclass(M)dim(M)length(M); stopifnot(length(M) == prod(dim(M)))nnzero(M) # more relevant than length## the above are also visible fromstr(M)}\keyword{attribute}