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]]
Hello, Try x <- matrix(1:18, ncol = 6) sapply(seq_len(ncol(x)), function(i) x[, i] - rowMeans(x[, -i])) Hope this helps, Rui Barradas Em 27-11-2012 17:51, eliza botto escreveu:> 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 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.
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_botto at hotmail.com> To: "r-help at r-project.org" <r-help at 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 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.
HI Eliza, No problem. You could also use: set.seed(5) mat1<-matrix(sample(1:400,80,replace=TRUE),ncol=8,nrow=10) library(plyr) resnew<-do.call(cbind,lapply(alply(mat1,2),function(x) x-colMeans(t(mat1)[!colnames(mat1)%in%names(alply(mat1,2))[match.call()[[2]][[3]]],]))) resprev<-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}))) ?identical(resprev,resnew) #[1] TRUE A.K. ________________________________ From: eliza botto <eliza_botto at hotmail.com> To: "smartpink111 at yahoo.com" <smartpink111 at yahoo.com> Sent: Tuesday, November 27, 2012 1:53 PM Subject: RE: [R] loop command to matrix thanks arun. i m more interested in learning commands.? thankyou so much eliza> Date: Tue, 27 Nov 2012 10:45:37 -0800 > From: smartpink111 at yahoo.com > Subject: Re: [R] loop command to matrix > To: eliza_botto at hotmail.com > CC: ruipbarradas at sapo.pt > > HI Eliza, > > I just saw Rui's solution.? It is much simpler than mine. > 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: 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 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. >