listarrays
A toolbox for working with R arrays in a functional programming style. Flexibly split, bind, reshape, modify, subset, and name arrays.
The package provides:
-
split_on_dim()andsplit_along_dim()which take an array and return a list. -
bind_on_dim()andbind_as_dim()take a list and return an array. -
modify_along_dim()takes an array, calls the passed function.f()on each subset of the specified dimension, and returns an array of the same shape. (think of this as a safer and sometimes faster alternative tobase::apply()that is guaranteed to return an array of the same shape as it received) -
extract_dim()a wrapper around[that allows you to specify the dimension being subset as a function argument. For example,extract_dim(X, 1, idx)will extractidxon the first dimension, regardless how many dimensions are in the arrayX. Contrast this with the base alternativeX[idx,,], where you have to match the number of commas,to the number of dimensions inX. -
Many of the functions have two variants
*_rows()and*_cols()for the two most common case of the first and last dimension. For examplesplit_on_rows()which is equivalent tosplit_on_dim(X, 1)andsplit_on_cols()which is equivalent tosplit_on_dim(X, -1) -
set_dim()andset_dimnames(), pipe-friendly and more flexible versions ofdim<-anddimnames<- -
dim2()<-,set_dim2(),array2(), which reshape or fills arrays using row-major (C-style) semantics -
A handful of lower-level helpers that abstract out patterns commonly encountered while working with arrays, for example
expand_dims()(the inverse ofbase::drop(), orseq_along_rows()(a combination ofseq_along()andnrow()). -
A set of functions that help encode atomic vectors as
onehot()binary matrix’s anddecode_onehot()back into atomic vectors. (for example if training a neural network with keras) -
Many of the functions work recursively if provided a list of arrays.
Installation
You can install listarrays from CRAN with:
install.packages("listarrays")Or install the development version from github with:
devtools::install_github("t-kalinowski/listarrays")