[R] apply to multiple arrays simultaneously

Gabor Grothendieck ggrothendieck at myway.com
Sat Jan 10 00:51:47 CET 2004




Try something like this:

mat.a1 <- apply( a1, 1:2, list )
mat.a2 <- apply( a2, 1:2, list )
mapply( my.function, a1, a2 )

Note that mat.a1 and mat.a2 are 3x4 matrices,
each element of which holds a matrix.


Date:   Fri, 09 Jan 2004 10:10:03 +0000 
From:   Carlos Soares <csoares at liacc.up.pt>
To:   <r-help at stat.math.ethz.ch> 
Subject:   [R] apply to multiple arrays simultaneously 

 
Dear R users,

Suppose two arrays which partly have the same dimensions. For instance, 
a1 and a2 with dim(a1) is c(3,4,5,6) and dim(a2) is c(3,4,7,8). How can 
I perform an apply on the equivalent part of the dimensions on both 
arrays simultaneously? Something like:

apply(list(a1, a2), c(1,2), function(x,y) {my.function(x,y)})

A useful bonus would be, assuming that my.function always returns an 
array withthe same dimensions (e.g., c(2,3,4), that the final result 
would be an array wit
h dimensions c(3,4,2,3,4).

With best regards,
Carlos

______________________________________________
R-help at stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html




More information about the R-help mailing list