is_nonzero | R Documentation |
A set of functions for direct manipulation of the nonzero elements of an array-like object.
is_nonzero(x)
nzcount(x)
nzwhich(x, arr.ind=FALSE)
nzvals(x)
nzvals(x) <- value
sparsity(x)
x |
Typically (but not necessarily) an array-like object that is sparse, like a SparseArray derivative, or a dg[C|R]Matrix or lg[C|R]Matrix object from the Matrix package. However, |
arr.ind |
If See Note that using |
value |
A vector, typically of length |
nzcount(x)
and nzwhich(x)
are equivalent to, but
typically more efficient than, sum(is_nonzero(x))
and
which(is_nonzero(x))
, respectively.
nzvals(x)
is equivalent to, but typically more efficient than,
x[nzwhich(x)]
(or x[is_nonzero(x)]
).
nzvals(x) <- value
replaces the values of the nonzero array
elements in x
with the supplied values. It's equivalent to,
but typically more efficient than, x[nzwhich(x)] <- value
.
Note that nzvals(x) <- nzvals(x)
is guaranteed to be a no-op.
is_nonzero()
: An array-like object of type()
"logical"
and same dimensions as the input object.
nzcount()
: The number of nonzero array elements in x
.
nzwhich()
: The indices of the nonzero array elements in x
,
either as an L-index (if arr.ind
is FALSE
) or as
an M-index (if arr.ind
is TRUE
).
Note that the indices are returned sorted in strictly ascending order.
nzvals()
: A vector of the same type()
as x
and
containing the values of the nonzero array elements in x
.
Note that the returned vector is guaranteed to be parallel
to nzwhich(x)
.
sparsity(x)
: The ratio between the number of zero-valued
elements in array-like object x
and its total number of
elements (length(x)
or prod(dim(x))
).
More precisely, sparsity(x)
is 1 - nzcount(x)/length(x)
.
is_nonna for is_nonna()
and nna*()
functions
nnacount()
, nnawhich()
, etc...
SparseArray objects.
S4 classes dgCMatrix, lgCMatrix, and ngCMatrix defined in the Matrix package.
Ordinary array objects in base R.
base::which
in base R.
a <- array(rpois(120, lambda=0.3), dim=c(5, 12, 2))
is_nonzero(a)
## Get the number of nonzero array elements in 'a':
nzcount(a)
## nzwhich() returns the indices of the nonzero array elements in 'a'.
## Either as a "L-index" i.e. an integer (or numeric) vector of
## length 'nzcount(a)' containing "linear indices":
nzidx <- nzwhich(a)
length(nzidx)
head(nzidx)
## Or as an "M-index" i.e. an integer matrix with 'nzcount(a)' rows
## and one column per dimension where the rows represent "array indices"
## (a.k.a. "array coordinates"):
Mnzidx <- nzwhich(a, arr.ind=TRUE)
dim(Mnzidx)
## Each row in the matrix is an n-tuple representing the "array
## coordinates" of a nonzero element in 'a':
head(Mnzidx)
tail(Mnzidx)
## Extract the values of the nonzero array elements in 'a' and return
## them in a vector "parallel" to 'nzwhich(a)':
a_nzvals <- nzvals(a) # equivalent to 'a[nzwhich(a)]'
length(a_nzvals)
head(a_nzvals)
nzvals(a) <- log1p(nzvals(a))
a
## Sanity checks:
stopifnot(
identical(nzidx, which(a != 0)),
identical(Mnzidx, which(a != 0, arr.ind=TRUE, useNames=FALSE)),
identical(nzvals(a), a[nzidx]),
identical(nzvals(a), a[Mnzidx]),
identical(`nzvals<-`(a, nzvals(a)), a)
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.