modify_along_dim: Modify an array by mapping over 1 or more dimensions

Description Usage Arguments Value Examples

View source: R/modify.R

Description

This function can be thought of as a version of base::apply() that is guaranteed to return a object of the same dimensions as it was input. It also generally preserves attributes, as it's built on top of [<-.

Usage

1
2
3
4
5
modify_along_dim(X, which_dim, .f, ...)

modify_along_rows(X, .f, ...)

modify_along_cols(X, .f, ...)

Arguments

X

An array, or a list of arrays

which_dim

integer vector of dimensions to modify at

.f

a function or formula defining a function(same semantics as purrr::map()). The function must return either an array the same shape as it was passed, a vector of the same length, or a scalar, although the type of the returned object does not need to be the same as was passed in.

...

passed on to .f()

Value

An array, or if X was a list, a list of arrays of the same shape as was passed in.

Examples

1
2
3
x <- array(1:6, 1:3)
modify_along_dim(x, 3, ~mean(.x))
modify_along_dim(x, 3, ~.x/mean(.x))

listarrays documentation built on March 26, 2020, 6:10 p.m.