is_nonna | R Documentation |
A set of functions for direct manipulation of the non-NA elements of an array-like object.
is_nonna(x)
nnacount(x)
nnawhich(x, arr.ind=FALSE)
nnavals(x)
nnavals(x) <- value
x |
Typically (but not necessarily) an array-like object that is non-NA sparse, like an NaArray object. However, |
arr.ind |
If See Note that using |
value |
A vector, typically of length |
nnacount(x)
and nnawhich(x)
are equivalent to, but
typically more efficient than, sum(is_nonna(x))
and
which(is_nonna(x))
, respectively.
nnavals(x)
is equivalent to, but typically more efficient than,
x[nnawhich(x)]
(or x[is_nonna(x)]
).
nnavals(x) <- value
replaces the values of the non-NA array
elements in x
with the supplied values. It's equivalent to,
but typically more efficient than, x[nnawhich(x)] <- value
.
Note that nnavals(x) <- nnavals(x)
is guaranteed to be a no-op.
is_nonna()
: An array-like object of type()
"logical"
and same dimensions as the input object.
nnacount()
: The number of non-NA array elements in x
.
nnawhich()
: The indices of the non-NA 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.
nnavals()
: A vector of the same type()
as x
and
containing the values of the non-NA array elements in x
.
Note that the returned vector is guaranteed to be parallel
to nnawhich(x)
.
is_nonzero for is_nonzero()
and nz*()
functions
nzcount()
, nzwhich()
, etc...
NaArray objects.
Ordinary array objects in base R.
base::which
in base R.
a <- array(NA_integer_, dim=c(5, 12, 2))
a[sample(length(a), 20)] <- (-9):10
is_nonna(a)
## Get the number of non-NA array elements in 'a':
nnacount(a)
## nnawhich() returns the indices of the non-NA array elements in 'a'.
## Either as a "L-index" i.e. an integer (or numeric) vector of
## length 'nnacount(a)' containing "linear indices":
nnaidx <- nnawhich(a)
length(nnaidx)
head(nnaidx)
## Or as an "M-index" i.e. an integer matrix with 'nnacount(a)' rows
## and one column per dimension where the rows represent "array indices"
## (a.k.a. "array coordinates"):
Mnnaidx <- nnawhich(a, arr.ind=TRUE)
dim(Mnnaidx)
## Each row in the matrix is an n-tuple representing the "array
## coordinates" of a non-NA element in 'a':
head(Mnnaidx)
tail(Mnnaidx)
## Extract the values of the non-NA array elements in 'a' and return
## them in a vector "parallel" to 'nnawhich(a)':
a_nnavals <- nnavals(a) # equivalent to 'a[nnawhich(a)]'
length(a_nnavals)
head(a_nnavals)
nnavals(a) <- 10 ^ nnavals(a)
a
## Sanity checks:
stopifnot(
identical(nnaidx, which(!is.na(a))),
identical(Mnnaidx, which(!is.na(a), arr.ind=TRUE, useNames=FALSE)),
identical(nnavals(a), a[nnaidx]),
identical(nnavals(a), a[Mnnaidx]),
identical(`nnavals<-`(a, nnavals(a)), a)
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.