[R] Working with Matrix

Christofer Bogaso bogaso.christofer at gmail.com
Fri Jan 4 12:47:40 CET 2013


Hello again,

Let say I have 2 matrices which equal number of columns but different
number of rows like:

Mat1 <- matrix(1:20, 4, 5)
Mat2 <- matrix(1:25, 5, 5)

Now for each column 1-to-5 I need to fetch the corresponding columns
of these 2 matrices and add the corresponding elements (ignoring NA
values if any). Therefore for the 1st column I need to do:

(1+1), (2+2),...,(4+4), (NA+5)

and so on

And the resulting numbers will be stored in some other matrix

Also note that, here I gave the example of addition, however, this can
be any user defined function.

Is there any R way to do that, without using any for-loop?

Thanks and regards,




More information about the R-help mailing list