Hello, I am working on seperate the matrix to two matrices but got trouble on doing it. Please give me some suggestions on doing this. Thanks a looooooooooooot! My original matrix m is as follows for example, [,1] [,2] [,3] [1,] 6 8 1 [2,] 5 9 2 [3,] 20 10 3 [4,] 7 11 4 [5,] 8 12 5 [6,] 25 13 6 [7,] 14 14 7 I want to generate two matrix m1 and m2 if m[i, 1] <m[i,2], then the row is stored in new matrix m1, (i =1, ..., 7) if m[i,1]>=m[i,2], then the row is stored in new matrix m2. m1 supposed to be like: [,1] [,2] [,3] [1,] 6 8 1 [2,] 5 9 2 [3,] 7 11 4 [4,] 8 12 5 m2 supposed to be like: [,1] [,2] [,3] [1,] 20 10 3 [2,] 25 13 6 [3,] 14 14 7 How can I code the if statement? Thank you very much -- View this message in context: http://n4.nabble.com/how-to-seperate-a-matrix-tp948225p948225.html Sent from the R help mailing list archive at Nabble.com.
Hi there, mymat<-matrix(c(6,8,1, 5,9,2, 20,10,3, 7,11,4, 8,12,5, 25,13,6, 14,14,7), byrow=T, ncol=3) mymat1<-mymat[mymat[,1]<mymat[,2],] mymat1 mymat2<-mymat[mymat[,1]>=mymat[,2],] mymat2 bests milton On Thu, Dec 3, 2009 at 10:12 PM, aegea <gcheer3@gmail.com> wrote:> > Hello, > > I am working on seperate the matrix to two matrices but got trouble on > doing > it. Please give me some suggestions on doing this. Thanks a > looooooooooooot! > > My original matrix m is as follows for example, > > [,1] [,2] [,3] > [1,] 6 8 1 > [2,] 5 9 2 > [3,] 20 10 3 > [4,] 7 11 4 > [5,] 8 12 5 > [6,] 25 13 6 > [7,] 14 14 7 > > I want to generate two matrix m1 and m2 > if m[i, 1] <m[i,2], then the row is stored in new matrix m1, (i =1, ..., 7) > if m[i,1]>=m[i,2], then the row is stored in new matrix m2. > > m1 supposed to be like: > [,1] [,2] [,3] > [1,] 6 8 1 > [2,] 5 9 2 > [3,] 7 11 4 > [4,] 8 12 5 > > m2 supposed to be like: > [,1] [,2] [,3] > [1,] 20 10 3 > [2,] 25 13 6 > [3,] 14 14 7 > > > How can I code the if statement? Thank you very much > > -- > View this message in context: > http://n4.nabble.com/how-to-seperate-a-matrix-tp948225p948225.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Hi aegea, Here is one: m <- structure(c(6, 5, 20, 7, 8, 25, 14, 8, 9, 10, 11, 12, 13, 14, 1, 2, 3, 4, 5, 6, 7), .Dim = c(7L, 3L)) m1 <- m[m[,1] >= m[,2], ] m1 m2 <- m[m[,1] < m[,2], ] m2 Best, Jorge On Thu, Dec 3, 2009 at 10:12 PM, aegea <> wrote:> > Hello, > > I am working on seperate the matrix to two matrices but got trouble on > doing > it. Please give me some suggestions on doing this. Thanks a > looooooooooooot! > > My original matrix m is as follows for example, > > [,1] [,2] [,3] > [1,] 6 8 1 > [2,] 5 9 2 > [3,] 20 10 3 > [4,] 7 11 4 > [5,] 8 12 5 > [6,] 25 13 6 > [7,] 14 14 7 > > I want to generate two matrix m1 and m2 > if m[i, 1] <m[i,2], then the row is stored in new matrix m1, (i =1, ..., 7) > if m[i,1]>=m[i,2], then the row is stored in new matrix m2. > > m1 supposed to be like: > [,1] [,2] [,3] > [1,] 6 8 1 > [2,] 5 9 2 > [3,] 7 11 4 > [4,] 8 12 5 > > m2 supposed to be like: > [,1] [,2] [,3] > [1,] 20 10 3 > [2,] 25 13 6 > [3,] 14 14 7 > > > How can I code the if statement? Thank you very much > > -- > View this message in context: > http://n4.nabble.com/how-to-seperate-a-matrix-tp948225p948225.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >[[alternative HTML version deleted]]