Re: [R] *apply function for arrays?

From: Hesen Peng <>
Date: Mon, 19 May 2008 08:28:42 +0800


Thank you so much for the help. apply does work for the first situation.

For the second situation, I'm currently using:


And I wonder whether there are some other ways to do this.

On Sun, May 18, 2008 at 3:52 PM, Patrick Burns <> wrote:
> You want to use the 'apply' function. S Poetry
> has a chapter on higher dimensional arrays that
> may be of use to you.
> I don't see a good approach for your second
> situation other than a double for loop. Unless
> G is linear, I think all other solutions will be
> equivalent to a double for loop, though the
> loops may be hidden.
> Patrick Burns
> +44 (0)20 8525 0696
> (home of S Poetry and "A Guide for the Unwilling S User")
> Hesen Peng wrote:
>> Hi all,
>> I've recently been writing functions which may deal with very large
>> arrays. And I hope to use *apply functions in the program so that the
>> code may look nicer and the performance may be better in the following
>> two situations.
>> The first situation is:
>> I'm having an array A with dim(A)==c(m,n,p). And I want to apply a
>> function F to a group of elements in A like:
>> 1) F is applied to every group of elements like A[i,j,] for all i and j.
>> or
>> 2) F is applied to every group of elements like A[i,,] for all i, or
>> A[,j,] for all j.
>> The result is then expected to be a m*n-dimensional matrix
>> I'm currently using for loops to do this but I guess there maybe some
>> *apply functions for arrays which may accomplish this task.
>> The second situation is:
>> I'm having two matrix M and N and a function G, which is a function of
>> two arrays. I want to form a matrix R with dim(R)==c(nrow(M),nrow(N)),
>> and R[i,j] <- G(M[i,], N[j,]). And I wonder whether I may use some
>> functions like outer to the matrix.
>> Thank you very much and have a nice weekend.

彭河森 Hesen Peng
______________________________________________ mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.
Received on Mon 19 May 2008 - 00:36:12 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 19 May 2008 - 09:30:38 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive