Skip to content
Prev 172814 / 398506 Next

How to apply a function to slices of multidimensional arrays, and not just iterating through single elements

well, you can still use apply(), e.g.,

A <- array(rnorm(3*4*5), c(3, 4, 5))
f <- sum

out <- numeric(4)
for (i in 1:4)
     out[i] <- f(A[, i, ])
out
apply(A, 2, f)


I hope it helps.

Best,
Dimitris
Todor Kondic wrote: