[R] Applying a function to an array

Tsjerk Wassenaar tsjerkw at gmail.com
Fri Aug 24 15:54:53 CEST 2007



apply( a, c(1,2), sd )



On 8/24/07, Florent Bresson <f_bresson at yahoo.fr> wrote:
> 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 ?
> Thanks
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

Tsjerk A. Wassenaar, Ph.D.
Junior UD (post-doc)
Biomolecular NMR, Bijvoet Center
Utrecht University
Padualaan 8
3584 CH Utrecht
The Netherlands
P: +31-30-2539931
F: +31-30-2537623

More information about the R-help mailing list