Gundala Viswanath
2008-Aug-01 03:00 UTC
[R] Grouping Index of Matrix Based on Certain Condition
Hi, I have the following (M x N) matrix, where M = 10 and N =2 What I intend to do is to group index of (M) based on this condition of "x_mn" , namely For each M, If x_m1 > x_m2, assign index of M to Group1 otherwise assign index of M into Group 2> x[,1] [,2] [1,] 4.482909e-01 0.55170907 [2,] 9.479594e-01 0.05204063 [3,] 8.923553e-01 0.10764474 [4,] 9.295003e-01 0.07049966 [5,] 8.880434e-01 0.11195664 [6,] 9.197367e-01 0.08026327 [7,] 9.431232e-01 0.05687676 [8,] 9.460356e-01 0.05396442 [9,] 6.053829e-01 0.39461708 [10,] 9.515173e-01 0.04848268 Is there a simple way to do it in R? - Gundala Viswanath Jakarta - Indonesia
Christos Hatzis
2008-Aug-01 03:26 UTC
[R] Grouping Index of Matrix Based on Certain Condition
Try this:> x <- matrix(runif(20), ncol=2) > x[,1] [,2] [1,] 0.33119833 0.4797847 [2,] 0.01339784 0.5218626 [3,] 0.78975940 0.8597246 [4,] 0.60849015 0.5217248 [5,] 0.91779777 0.9364047 [6,] 0.88302538 0.3467961 [7,] 0.87565986 0.4029147 [8,] 0.51594479 0.9885018 [9,] 0.87680383 0.7694989 [10,] 0.40761877 0.4661846> apply(x, 1, function(y) ifelse(diff(y)<0, 1, 2))[1] 2 2 2 1 2 1 1 2 1 2 -Christos> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Gundala Viswanath > Sent: Thursday, July 31, 2008 11:00 PM > To: r-help at stat.math.ethz.ch > Subject: [R] Grouping Index of Matrix Based on Certain Condition > > Hi, > > I have the following (M x N) matrix, where M = 10 and N =2 > What I intend to do is to group index of (M) based on this > condition of "x_mn" , namely > > For each M, > If x_m1 > x_m2, assign index of M to Group1 otherwise assign > index of M into Group 2 > > > > x > [,1] [,2] > [1,] 4.482909e-01 0.55170907 > [2,] 9.479594e-01 0.05204063 > [3,] 8.923553e-01 0.10764474 > [4,] 9.295003e-01 0.07049966 > [5,] 8.880434e-01 0.11195664 > [6,] 9.197367e-01 0.08026327 > [7,] 9.431232e-01 0.05687676 > [8,] 9.460356e-01 0.05396442 > [9,] 6.053829e-01 0.39461708 > [10,] 9.515173e-01 0.04848268 > > Is there a simple way to do it in R? > - Gundala Viswanath > Jakarta - Indonesia > > ______________________________________________ > 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. > >
Jorge Ivan Velez
2008-Aug-01 03:34 UTC
[R] Grouping Index of Matrix Based on Certain Condition
Dear Gundala, Try this: x=as.matrix(read.table(textConnection(" 4.482909e-01 0.55170907 9.479594e-01 0.05204063 8.923553e-01 0.10764474 9.295003e-01 0.07049966 8.880434e-01 0.11195664 9.197367e-01 0.08026327 9.431232e-01 0.05687676 9.460356e-01 0.05396442 6.053829e-01 0.39461708 9.515173e-01 0.04848268"),header=FALSE,sep="")) index=apply(x,1,function(x) ifelse(x[1]>x[2],'Group1','Group2')) index [1] "Group2" "Group1" "Group1" "Group1" "Group1" "Group1" "Group1" "Group1" "Group1" "Group1" HTH, Jorge On Thu, Jul 31, 2008 at 11:00 PM, Gundala Viswanath <gundalav@gmail.com>wrote:> Hi, > > I have the following (M x N) matrix, where M = 10 and N =2 > What I intend to do is to group index of (M) based on this condition > of "x_mn" , namely > > For each M, > If x_m1 > x_m2, assign index of M to Group1 > otherwise assign index of M into Group 2 > > > > x > [,1] [,2] > [1,] 4.482909e-01 0.55170907 > [2,] 9.479594e-01 0.05204063 > [3,] 8.923553e-01 0.10764474 > [4,] 9.295003e-01 0.07049966 > [5,] 8.880434e-01 0.11195664 > [6,] 9.197367e-01 0.08026327 > [7,] 9.431232e-01 0.05687676 > [8,] 9.460356e-01 0.05396442 > [9,] 6.053829e-01 0.39461708 > [10,] 9.515173e-01 0.04848268 > > Is there a simple way to do it in R? > - Gundala Viswanath > Jakarta - Indonesia > > ______________________________________________ > 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]]
Moshe Olshansky
2008-Aug-01 04:23 UTC
[R] Grouping Index of Matrix Based on Certain Condition
y <- 2 - (x[,1] > x[,2]) you can also do cbind(x,y) if you wish. --- On Fri, 1/8/08, Gundala Viswanath <gundalav at gmail.com> wrote:> From: Gundala Viswanath <gundalav at gmail.com> > Subject: [R] Grouping Index of Matrix Based on Certain Condition > To: r-help at stat.math.ethz.ch > Received: Friday, 1 August, 2008, 1:00 PM > Hi, > > I have the following (M x N) matrix, where M = 10 and N =2 > What I intend to do is to group index of (M) based on this > condition > of "x_mn" , namely > > For each M, > If x_m1 > x_m2, assign index of M to Group1 > otherwise assign index of M into Group 2 > > > > x > [,1] [,2] > [1,] 4.482909e-01 0.55170907 > [2,] 9.479594e-01 0.05204063 > [3,] 8.923553e-01 0.10764474 > [4,] 9.295003e-01 0.07049966 > [5,] 8.880434e-01 0.11195664 > [6,] 9.197367e-01 0.08026327 > [7,] 9.431232e-01 0.05687676 > [8,] 9.460356e-01 0.05396442 > [9,] 6.053829e-01 0.39461708 > [10,] 9.515173e-01 0.04848268 > > Is there a simple way to do it in R? > - Gundala Viswanath > Jakarta - Indonesia > > ______________________________________________ > 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.