[R] Multi matrix row-wise mapply?

hadley wickham h.wickham at gmail.com
Tue Oct 21 21:15:27 CEST 2008


On Tue, Oct 21, 2008 at 1:58 AM, cmr.Pent at gmail.com <cmr.Pent at gmail.com> wrote:
> Hi group!
>
> Suppose I have 2 matrices A and B of equal dimensions.
> I want to apply a function f to all corresponding pairs of rows from A
> and B in an efficient manner.
> Basically, I want
>
> mapply(f, data.frame(A), data.frame(B))
>
> but for rows.

You could make a 3d array:

A <- matrix(sample(100), 10)
B <- matrix(sample(100), 10)

library(abind)
AB <- abind(A, B, rev.along = 0)

apply(AB, 1, myfunction)

Hadley

-- 
http://had.co.nz/



More information about the R-help mailing list