[R] Applying a function to an array

Florent Bresson f_bresson at yahoo.fr
Fri Aug 24 15:18:35 CEST 2007

Dear R-users,

I would like to apply a function (more precisely sd()) over the third dimension of a three-dimension array. The function apply would be interesting but the chosen function can only be applied on the rows and columns of the array according to the help file. I can use a loop to cut the array in matrices and then use apply for each replication, but it's not very nice. A small example:

a    <-    array(runif(1200),c(3,4,100))
b    <-    matrix(NA,3,4)
for(j in 1:3){ b[j,]    <-    apply(a[j,,],1,sd) }

Is there someting better than the use of a loop ?


More information about the R-help mailing list