Learn R Programming

DiceView (version 2.2-0)

Apply.function: 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,
  ...
)

Value

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

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.

Examples

Run this code
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)

Run the code above in your browser using DataLab