[R] Median across matrices

arun smartpink111 at yahoo.com
Sat Mar 23 00:14:12 CET 2013


HI,
Try this:
set.seed(15)
lst<- list(matrix(sample(1:15,20,replace=TRUE),ncol=5),matrix(sample(4:20,20,replace=TRUE),ncol=5),matrix(sample(8:25,20,replace=TRUE),ncol=5))
library(abind)
arr1<-abind(lst,along=3)
apply(arr1,c(1,2),median)
#    [,1] [,2] [,3] [,4] [,5]
#[1,]   17   13   19   12    7
#[2,]   16   15   12   11   15
#[3,]   15   13   12   12    9
#[4,]   10    6   10   15   12

#or
library(plyr)
 aaply(laply(lst,as.matrix),c(2,3),median)
#   X2
#X1   1  2  3  4  5
#  1 17 13 19 12  7
#  2 16 15 12 11 15
#  3 15 13 12 12  9
#  4 10  6 10 15 12
A.K.




----- Original Message -----
From: Lucas Holland <hollandlucas at gmail.com>
To: "r-help at R-project.org" <r-help at r-project.org>
Cc: 
Sent: Friday, March 22, 2013 5:17 PM
Subject: [R] Median across matrices

Hey all,

I have a list of matrices. I'd like to calculate the median across all those matrices for each element. What I'd like to end up with is a matrix containing the median of all [1,1] [1,2] etc. elements across all matrices.

Is there a concise way of doing that?

Thanks!

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list