iapply {reshape}R Documentation

Idempotent apply

Description

A version of apply that works like apply, but returns the array in the same shape as the original. This is useful in conjunction with stamp.

Usage

iapply(x, margins=1, fun, ..., DROP=FALSE, COPY.DIMNAMES=FALSE, REORDER=TRUE)

Arguments

x array
margins margins to apply over
fun function to apply
... other arguments pass to function
DROP remove extraneous (length 1) dimensions?
COPY.DIMNAMES use original dimnames?
REORDER

Details

iapply is idempotent in the sense that iapply(x, a, function(x) x) will always return x for any value a. This is different to apply, which returns a permutation of the original matrix.

fun should return an array, matrix or vector.

Author(s)

Hadley Wickham <h.wickham@gmail.com>

See Also

apply for the function which this is based on

Examples

a <- array(1:27, c(2,3,4))
all.equal(a, iapply(a, 1, force))
all.equal(a, iapply(a, 2, force))
all.equal(a, iapply(a, 3, force))
all.equal(a, iapply(a, 1:2, force))
all.equal(aperm(a, c(2,1,3)), iapply(a, 2, force, REORDER=FALSE))
all.equal(aperm(a, c(3,1,2)), iapply(a, 3, force, REORDER=FALSE))

iapply(a, 1, min)
iapply(a, 1, min, DROP=TRUE)
iapply(a, 2, min)
iapply(a, 2, min, DROP=TRUE)
iapply(a, 3, min)
iapply(a, 3, min, DROP=TRUE)
iapply(a, 1, range)
iapply(a, 2, range)
iapply(a, 3, range)

mina <- iapply(a, 1, min)
sweep(a, 1, mina)
mina <- iapply(a, c(1,3), min)
sweep(a, c(1,3), mina)

[Package reshape version 0.8.0 Index]