map_along_dim {listarrays}R Documentation

Apply a function across subsets along an array dimension

Description

map_along_dim(X, dim, func) is a simple wrapper around split_along_dim(X, dim) %>% map(func). It is conceptually and functionally equivalent to base::apply(), with the following key differences:

Usage

map_along_dim(X, .dim, .f, ...)

map_along_rows(X, .f, ...)

map_along_cols(X, .f, ...)

Arguments

X

an R array

.dim

which dimension to map along. Passed on to split_along_dim(), and accepts all the same inputs. Valid inputs include

  • positive integers (index position(s) of dimension),

  • negative integers (index positions(s) of dimensions, counting from the back), or

  • character vector (corresponding to array dimnames)

.f

A function, string of a function name, or purrr style compact lambda syntax (e.g, ~.x + 1)

...

passed on to .f()

Value

An R list

Examples

X <- matrix2(letters[1:15], ncol = 3)

apply(X, 1, function(x) paste(x, collapse = ""))   # simplifies to a vector
map_along_dim(X, 1, ~paste(.x, collapse = ""))     # returns a list

identical(
  map_along_rows(X, identity),
  map_along_dim(X, 1, identity)) # TRUE

identical(
  map_along_cols(X, identity),
  map_along_dim(X, -1, identity)) # TRUE

[Package listarrays version 0.4.0 Index]