[R] apply with two matrixes

Katalin Csillery csillery at selway.umt.edu
Wed Mar 12 22:34:54 CET 2003


I have a function which does a certain task with two vectors, 
f1 <- function(a,b){body}

I also have a list of matrixes (all with the same dim's), and for each
column of each matrix in the list I want to use "f1", in such way that
it gives the vector "a" in the first argument of "f1". The second argument
of the function "b" also comes form a column of matrix (same dim's as any
matrix in the list), but that matrix is the same for all matrixes in the
I use a separate function with an apply command to accomplish "f1" on each
column, and than lapply that function over all matrixes in the list.
The problem is I do not know how tell to apply to use the respective
columns of the single matrix over all matrixes in the list. 

Example (this does not work this way),
m #original matrix
l <- list(m1, m2, m3, ...) #list of matrixes
f1 <- function(a, b){body}
f2 <- function (mx) apply(mx, 2, f1, b = m)
lapply(l, f2)

What I want to end up with is a list of list (say lend), where
length(lend) = length(l)
length(lend[[any]]) = varies with some properties of the matrixes

My guess is that I sould use tapply, but I could not get it right.
The problem would be pretty easy with a loop, but efficiency is very
important, because the objects are huge and functions a computationally

Thanks for any help in advance!

Katalin Csillery
Division of Biological Sciences
University of Montana, Missoula MT 59801
Phone: 406 243 6106, E-mail: csillery at selway.umt.edu

More information about the R-help mailing list