arr_from_coo: Conveniently covert between coordinate-value and array...

View source: R/matrix.utils.R

arr_from_cooR 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 ncol(coord); if not given, inferred from dimnames.

x0

the default value.

dimnames

dimension name list.

X

an array.

na.rm

whether the NA elements of the array should be omitted from the list.

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 x0

coord

a matrix with a column for each dimension containing indexes of values distinct from x0

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

statnet/statnet.common documentation built on June 9, 2025, 2:34 a.m.