[R] Applying function to elements of matrices in a list
d.rizopoulos at erasmusmc.nl
Thu Nov 25 15:44:51 CET 2010
Reduce("+", x) / length(x)
On 11/25/2010 3:42 PM, statmobile wrote:
> Hello R-help,
> Please cc me on all responses, as I only receive summary emails from
> this list.
> I'm wondering if anybody has any tips on how to accomplish this
> efficiently. I have a list of matrices, and I'm trying to get the mean
> of the [i,j]'th element of each matrix in a list.
> So if I have a list of matrices, say
> x <- list(a=matrix(rnorm(4),nrow=2),b=matrix(rnorm(4),nrow=2))
> How would I get a 2x2 matrix, where the i,j'th element would be the mean
> across the the list of each of the i,j'th elements in the list? That is,
> where the [1,2] element would be the average of a[1,2] and b[1,2].
> Of course my list and matrices are much larger, and I was hoping there
> would be some trick with lapply that I may be missing here.
> R-help at r-project.org mailing list
> PLEASE do read the posting guide
> and provide commented, minimal, self-contained, reproducible code.
Department of Biostatistics
Erasmus University Medical Center
Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
More information about the R-help