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]]
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 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
m<-matrix(1:10,ncol=5)> m[,1] [,2] [,3] [,4] [,5] [1,] 1 3 5 7 9 [2,] 2 4 6 8 10 apply(m,2,function(x){median(x,m[,5])}) [1] 5.5 6.5 7.5 8.5 9.5 Though i am not 100% sure thats what you mean. But the median of each row between two columns kind of doesn't make much sense to me, its only 2 values then after all. On 15.10.2012, at 14:20, eliza botto wrote:> > 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 > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.