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
Off the top of my head, seems like you can abind() the two together and then run apply. See the abind package on CRAN. HTH, Andy> From: Carlos Soares > > 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------------------------------------------------------------------------------ Notice: This e-mail message, together with any attachments,...{{dropped}}
abind only allows one of the dimensions to be different. In may case they differ on several dimensions. For instance, in the example I gave (which was probably not clear enough), I would like my.function to be called 3*4 times, each time being passed 2 matrices x and y with dim(x)=c(5,6) and dim(y)=c(7,8). Anyway, thanks for the tip, Andy. Carlos Liaw, Andy wrote:>Off the top of my head, seems like you can abind() the two together and then >run apply. See the abind package on CRAN. > >HTH, >Andy > > > >>From: Carlos Soares >> >>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 >> >> > > >------------------------------------------------------------------------------ >Notice: This e-mail message, together with any attachments, contains >information of Merck & Co., Inc. (One Merck Drive, Whitehouse Station, New >Jersey, USA 08889), and/or its affiliates (which may be known outside the >United States as Merck Frosst, Merck Sharp & Dohme or MSD) that may be >confidential, proprietary copyrighted and/or legally privileged, and is >intended solely for the use of the individual or entity named on this message. >If you are not the intended recipient, and have received this message in >error, please immediately return this by e-mail and then delete it. >------------------------------------------------------------------------------ > >
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