# [R] median between matrix column

arun smartpink111 at yahoo.com
Mon Oct 15 14:55:55 CEST 2012

```HI,
May be this helps you (if I understand it correctly):
set.seed(1)
mat1<-matrix(sample(1:500,160,replace=TRUE),ncol=16)
res<-do.call(rbind,lapply(1:ncol(mat1[,-16]),function(i) median(sort(stack(as.data.frame(mat1[,c(i,16)]))[,1]))))
res
#       [,1]
#[1,] 239.0
#[2,] 238.0
#[3,] 181.5
#[4,] 244.5
#[5,] 265.0
#[6,] 212.0
#[7,] 228.5
#[8,] 233.0
#[9,] 182.5
#[10,] 239.5
#[11,] 233.5
#[12,] 220.0
#[13,] 235.0
#[14,] 259.0
#[15,] 227.0

A.K.

----- Original Message -----
From: eliza botto <eliza_botto at hotmail.com>
To: "r-help at r-project.org" <r-help at r-project.org>
Cc:
Sent: Monday, October 15, 2012 8:20 AM
Subject: [R] median between matrix column

Dear useRs,

1.    how to calculate single median value for two columns of a matrix?
2.    i have a matrix of 16 columns and 365 rows, how to calculate median between columns 1 and 16, 2 and 16, 3 and 16, 4 and 16, 5 and 16 till 15th column. is there a loop command to do the said operation?

regards
eliza
[[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help