Hi, is there a R function that order a matrix according to some criteria based on the rows(or cols) of that matrix? For example, let's say that my matrix S is composed by n rows S_1, S_2,.., S_n and that I compute some real value g_i=g(S_i) for each row. Then I want to order this set of g_i (from smaller to bigger) and order the correspondent row to the new position. Is it possible (apart from looping on the index) to do this with some predefined R function? Thanks, Francesco
look at 'order' yourMatrix[order(yourMatrix[, 'yourCol']), ] On Fri, Jan 21, 2011 at 2:38 PM, Francesco Petrogalli <francesco.petrogalli at gmail.com> wrote:> Hi, > is there a R function that order a matrix according to some criteria > based on the rows(or cols) of that matrix? > > For example, let's say that my matrix S is composed by n rows S_1, > S_2,.., S_n and that I compute some real value g_i=g(S_i) for each > row. > Then I want to order this set of g_i (from smaller to bigger) and > order the correspondent row to the new position. > > Is it possible (apart from looping on the index) to do this with some > predefined R function? > > Thanks, > > Francesco > > ______________________________________________ > 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. >-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve?
I think you want the following, assuming you defined your function g(): gValues = apply(S, 1, g); Sordered = S[order(gValues), ] Peter On Fri, Jan 21, 2011 at 11:38 AM, Francesco Petrogalli <francesco.petrogalli at gmail.com> wrote:> Hi, > is there a R function that order a matrix according to some criteria > based on the rows(or cols) of that matrix? > > For example, let's say that my matrix S is composed by n rows S_1, > S_2,.., S_n and that I compute some real value g_i=g(S_i) for each > row. > Then I want to order this set of g_i (from smaller to bigger) and > order the correspondent row to the new position. > > Is it possible (apart from looping on the index) to do this with some > predefined R function? > > Thanks, > > Francesco > > ______________________________________________ > 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. >