arr_from_coo {statnet.common} | R Documentation |
Conveniently covert between coordinate-value and array representations
Description
These function similarly to Matrix's utilities but is simpler and allows arbitrary baseline and handling of missing values. (It is also almost certainly much slower.) Also, since it is likely that operations will be performed on the elements of the array, their argument is first for easier piping.
Usage
arr_from_coo(x, coord, dim = lengths(dimnames), x0 = NA, dimnames = NULL)
arr_to_coo(X, x0, na.rm = FALSE)
Arguments
x |
values of elements differing from the default. |
coord |
an integer matrix of their indices. |
dim |
dimension vector; recycled to |
x0 |
the default value. |
dimnames |
dimension name list. |
X |
an array. |
na.rm |
whether the |
Details
If x0
is NA
, non-NA
elements are returned; if x0
is NULL
,
all elements are.
Value
coo_to_arr()
returns a matrix or an array.
arr_to_coo()
returns a list with the following elements:
x |
the values distinct from |
coord |
a matrix with a column for each dimension containing
indexes of values distinct from |
dim |
the dimension vector of the matrix |
dimnames |
the dimension name list of the matrix |
Examples
m <- matrix(rpois(25, 1), 5, 5)
arr_to_coo(m, 0L)
stopifnot(identical(do.call(arr_from_coo, arr_to_coo(m, 0L)), m))
stopifnot(length(arr_to_coo(m, NULL)$x) == 25) # No baseline
m[sample.int(25L, 2L)] <- NA
m
arr_to_coo(m, 0L) # Return NAs
arr_to_coo(m, 0L, na.rm = TRUE) # Drop NAs