[R] applying cbind (or any function) across all components in a list

David Winsemius dwinsemius at comcast.net
Thu May 24 20:25:59 CEST 2012


On May 24, 2012, at 12:49 PM, Hans Thompson wrote:

> I'm confused why I haven't made clear what I am asking for help with.
>
> I have two different lists with two (or many) components, [[1]] and  
> [[2]].
> One of the list has components with dim=c(2,3) and the other has  
> dim=c(2,2).
> I want to create a new list with components dim=c(2,4) by binding  
> together
> the averages of the columns using
>
>> cbind((l1[[1]][,1]+l2[[1]][,1])/2, (l1[[1]][,2]+l2[[1]][,1])/2,
>> (l1[[1]][,2]+l2[[1]][,2])/2, (l1[[1]][,3]+l2[[1]][,2])/2 )
>
> which should put out:
>
>     [,1] [,2] [,3] [,4]
> [1,]    1    2    3    4
> [2,]    2    3    4    5
>
> my problem is finding out how I can apply this function to all the
> components within the list in the same function.

Well, it's not clear what "function" you want applied to "what". In  
fact, it's even less clear in this posting than it was in your first  
one (since you assume, incorrectly, that we are looking at this on  
Nabble. Readers of this list expect you to include context.)  I could  
not figure out in your first posting how we should be considering the  
fourth expression

   {(l1[[1]][,3]+l2[[1]][,2])/2 }

... to be an "average" of anything since its indices do not match?  
Perhaps equivalently, why should a 2 x 3 structure merged <somehow> to  
a 2 x 2 structure yield a 2 x 4 structure?

-- 
David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list