Skip to content
Prev 172815 / 398506 Next

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

If I well understand, maybe you have still apply at your disposal:

arrayA <- 1:60
dim(arrayA) <- c(3,4,5)
apply(arrayA, 2, sum)

You have the same result of:
res<-numeric(4);for (i in 1:4) res[i]<-sum(arrayA[,i,])

Ciao,
domenico

PS:
have a look at plyr package for more "slicing" and "applying" functions
Todor Kondic wrote: