iapply {reshape} | R Documentation |
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
.
iapply(x, margins=1, fun, ..., DROP=FALSE, COPY.DIMNAMES=FALSE, REORDER=TRUE)
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 |
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.
Hadley Wickham <h.wickham@gmail.com>
apply
for the function which this is based on
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)