[R] Componentwise means of a list of matrices?

Stephan Kolassa Stephan.Kolassa at gmx.de
Tue Dec 30 15:33:21 CET 2008


Dear useRs,

I have a list, each entry of which is a matrix of constant dimensions. 
Is there a good way (i.e., not using a for loop) to apply a mean to each 
matrix entry *across list entries*?

Example:

foo <- list(rbind(c(1,2,3),c(4,5,6)),rbind(c(7,8,9),c(10,11,12)))
some.sort.of.apply(foo,FUN=mean)

I'm looking for a componentwise mean across the two entries of foo, 
i.e., the following output:

      [,1] [,2] [,3]
[1,]    4    5    6
[2,]    7    8    9

[NB. My "real" application involves trimming and psych::winsor(), so 
anything that generalizes to this would be extra good.]

I've been looking at apply and {s,l,m,t}apply, by, with and aggregate 
and searched the list archives... any ideas?

Thanks a lot,
Stephan



More information about the R-help mailing list