HI, May be this helps you: set.seed(5) mat1<-matrix(sample(1:400,80,replace=TRUE),ncol=8,nrow=10) split(mat1,col(mat1)) t(do.call(rbind,lapply(lapply(split(mat1,col(mat1)),function(x) cbind(matrix(x,ncol=1),mat1)),function(x){ res1<-rowMeans(t(apply(x,1,function(x) x[!(duplicated(x)|duplicated(x,fromLast=TRUE))]))) res<-x[,1]-res1 res})))
# 1 2 3 4 5 6 # [1,] -163.00000 -129.8571429 152.42857 -52.14286 118.142857 -79.57143 # [2,] 105.14286 16.0000000 121.14286 -181.71429 -100.571429 50.28571 # [3,] 162.14286 -111.0000000 -160.14286 -130.42857 121.000000 164.42857 # [4,] -123.71429 2.0000000 -150.00000 -239.14286 -9.428571 192.85714 [5,] -146.42857 -73.2857143 -130.42857 -187.57143 230.714286 233.00000 # [6,] 57.28571 -171.2857143 -44.42857 -41.00000 -12.428571 -89.00000 # [7,] 19.14286 -44.8571429 -23.14286 50.00000 125.428571 -105.42857 # [8,] 115.71429 152.2857143 187.71429 19.71429 -222.571429 -136.85714 # [9,] 184.85714 0.8571429 -187.71429 -70.00000 110.571429 -162.57143 #[10,] -189.85714 143.8571429 195.28571 -59.57143 49.000000 -178.42857 7 8 # [1,] 30.14286 123.85714 # [2,] -129.14286 118.85714 # [3,] 139.28571 -185.28571 # [4,] 198.57143 128.85714 # [5,] 257.00000 -183.00000 # [6,] 144.14286 156.71429 ## [7,] 183.71429 -204.85714 # [8,] -182.57143 66.57143 # [9,] 173.42857 -49.42857 #[10,] 137.00000 -97.28571 A.K. ----- Original Message ----- From: eliza botto <eliza_bo...@hotmail.com> To: "r-help@r-project.org" <r-help@r-project.org> Cc: Sent: Tuesday, November 27, 2012 12:51 PM Subject: [R] loop command to matrix Dear UseRs,Extremely sorry for a basic question. I have a matrix of 19 rows and 365 columns. what i want to do is the following...First i want to leave out column number 1 and want to calculate the row wise mean of the remaining columns, which will obviously give me 365 values in one column, and then subtracting these values from the column i left out i.e. col=1 then i want to leave out column 2 and calculate the row wise mean of the remaining columns which includes column 1 too and then subtracting these values from the column i left out i.e. col=2.and then continuing this process the last column. i know a kind of "manual way" of doing things but its extremely long and laborious.Is there any loop command or shorter way?? thanks in advanceregardseliza [[alternative HTML version deleted]] ______________________________________________ R-help@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. ______________________________________________ R-help@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.