Apply.function: Apply Functions Over Array Margins, using custom...

View source: R/function.R

Apply.functionR Documentation

Apply Functions Over Array Margins, using custom vectorization (possibly using parallel)

Description

Emulate parallel apply on a function, from mclapply. Returns a vector or array or list of values obtained by applying a function to margins of an array or matrix.

Usage

Apply.function(
  FUN,
  X,
  MARGIN = 1,
  .combine = c,
  .lapply = parallel::mclapply,
  ...
)

Arguments

FUN

function to apply on X

X

array of input values for FUN

MARGIN

1 indicates to apply on rows (default), 2 on columns

.combine

how to combine results (default using c(.))

.lapply

how to vectorize FUN call (default is parallel::mclapply)

...

optional arguments to FUN.

Value

array of values taken by FUN on each row/column of X

Examples

X = matrix(runif(10),ncol=2);
  rowSums(X) == apply(X,1,sum)
  apply(X,1,sum) == Apply.function(sum,X)

X = matrix(runif(10),ncol=1)
  rowSums(X) == apply(X,1,sum)
  apply(X,1,sum) == Apply.function(sum,X)

X = matrix(runif(10),ncol=2)
f = function(X) X[1]/X[2]
apply(X,1,f) == Apply.function(f,X)

IRSN/DiceView documentation built on Jan. 31, 2024, 10:09 a.m.