[R] Handling two lists of matrices

Marc Schwartz marc_schwartz at comcast.net
Tue Oct 9 20:09:18 CEST 2007


On Tue, 2007-10-09 at 13:34 -0400, Bornman, Daniel M wrote:
> I'm having trouble setting up the function call to handle two lists of
> matrices.  Each list has 6 matrices - Each matrix is 20x10.  I need to
> do some basic math on corresponding matrices in each list.
> 
> Here are some outputs of these lists, etc...
> 
> # first list
> > length(qc.pm)
> [1] 6
> > dim(qc.pm[[1]])
> [1] 20 10
> > qc.pm[[1]][1:4,1:4]
>        441-JP071707.CEL 442-JP071707.CEL 443-JP071707.CEL
> 444-JP071707.CEL
> 495086            11923             9209             7980
> 10088
> 31276              6883             5577             3985
> 5561
> 6479               4730             3700             2170
> 4772
> 673616             9495             8015             5252
> 8178
> 
> 
> # second list
> > length(qc.mm)
> [1] 6
> > dim(qc.mm[[1]])
> [1] 20 10
> > qc.pm[[1]][1:4,1:4]
>        441-JP071707.CEL 442-JP071707.CEL 443-JP071707.CEL
> 444-JP071707.CEL
> 495086            11923             9209             7980
> 10088
> 31276              6883             5577             3985
> 5561
> 6479               4730             3700             2170
> 4772
> 673616             9495             8015             5252
> 8178
> 
> 
> I'm having trouble passing 2 args to a function call to use the lapply()
> method.  Any suggestion would be appreciated.

Dan

If each of the lists contain the SAME number of matrices, that is:

  length(qc.pm) == length(qc.mm)

then you could do something like this:

  sapply(seq(length(qc.pm)), 
         function(x) DoSomethingWith(qc.pm[[x]], qc.mm[[x]]))


Alternatively, you could do:

  DoSomethingWith(x)
  {
    DoIt(qc.pm[[x]], qc.mm[[x]])
  }

  mapply(DoSomethingWith, x = seq(length(qc.pm)))


Which approach you take is to some extent style and the structure of the
returned result.

HTH,

Marc Schwartz



More information about the R-help mailing list