Dear All, a short and maybe simple question: I have to rank all values in a matrix from 0 to X, [1] [2] [3] [4] [1] 0.1 2 0 3 [2] 50 3 3 1 [3] 100 1 1 0 [4] 100 2 2 0 0->0 0.1->1 2->2 3->3 50->4 100->5 (X=5) is there any function for this? i have looked in several packages (vegan, labdsv etc.) because I am working with species by site tables, but without success. perhaps I looked for the wrong terms (rank matrix etc.) thank you for your help regards, Jens -- +++++++++++++++++++++++++++++++++++++++++ Dipl.Biol. Jens Oldeland University of Hamburg Biocentre Klein Flottbek and Botanical Garden Ohnhorststr. 18 22609 Hamburg, Germany Tel: 0049-(0)40-42816-407 Fax: 0049-(0)40-42816-543 Mail: Oldeland at botanik.uni-hamburg.de Jens.Oldeland at DLR.de (for attachments > 2mb!!) http://www.biologie.uni-hamburg.de/bzf/fbda005/fbda005.htm +++++++++++++++++++++++++++++++++++++++++
If your matrix below is called aa, you could do this> matrix(rank(aa, ties='min'), ncol=ncol(aa))[,1] [,2] [,3] [,4] [1,] 4 8 1 11 [2,] 14 11 11 5 [3,] 15 5 5 1 [4,] 15 8 8 1 This doesn't assign the values you list, but it does rank them. OTOH, if you want as you have below, I think this works rank(unique(stack(aa)[1]))-1> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Jens Oldeland > Sent: Monday, May 19, 2008 7:47 AM > To: r-help at stat.math.ethz.ch > Subject: [R] Rank Values in a Matrix > > Dear All, > > a short and maybe simple question: > > > I have to rank all values in a matrix from 0 to X, > > [1] [2] [3] [4] > [1] 0.1 2 0 3 > [2] 50 3 3 1 > [3] 100 1 1 0 > [4] 100 2 2 0 > > 0->0 > 0.1->1 > 2->2 > 3->3 > 50->4 > 100->5 (X=5) > > is there any function for this? i have looked in several > packages (vegan, labdsv etc.) because I am working with > species by site tables, but without success. perhaps I looked > for the wrong terms (rank matrix etc.) > > thank you for your help > > regards, > Jens > > -- > +++++++++++++++++++++++++++++++++++++++++ > Dipl.Biol. Jens Oldeland > University of Hamburg > Biocentre Klein Flottbek and Botanical Garden Ohnhorststr. 18 > 22609 Hamburg, > Germany > > Tel: 0049-(0)40-42816-407 > Fax: 0049-(0)40-42816-543 > Mail: Oldeland at botanik.uni-hamburg.de > Jens.Oldeland at DLR.de (for attachments > 2mb!!) > http://www.biologie.uni-hamburg.de/bzf/fbda005/fbda005.htm > +++++++++++++++++++++++++++++++++++++++++ > > ______________________________________________ > 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 Jens, If the matrix you described is m. Then, you can do > unique(as.vector(m)) [1] 0.1 2.0 0.0 3.0 50.0 1.0 100.0 > order(unique(as.vector(m))) [1] 3 1 6 2 4 5 7 Hope this helps :) Naira Jens Oldeland wrote:> Dear All, > > a short and maybe simple question: > > > I have to rank all values in a matrix from 0 to X, > > [1] [2] [3] [4] > [1] 0.1 2 0 3 > [2] 50 3 3 1 > [3] 100 1 1 0 > [4] 100 2 2 0 > > 0->0 > 0.1->1 > 2->2 > 3->3 > 50->4 > 100->5 (X=5) > > is there any function for this? i have looked in several packages > (vegan, labdsv etc.) because I am working with species by site tables, > but without success. perhaps I looked for the wrong terms (rank matrix > etc.) > > thank you for your help > > regards, > Jens >-- =================================================================Naira Naouar Tel:+32 (0)9 331 38 63 VIB Department of Plant Systems Biology, Ghent University Technologiepark 927, 9052 Gent, BELGIUM nanao at psb.ugent.be http://www.psb.ugent.be
On 05/19/08 13:46, Jens Oldeland wrote:> Dear All, > > a short and maybe simple question: > > > I have to rank all values in a matrix from 0 to X, > > [1] [2] [3] [4] > [1] 0.1 2 0 3 > [2] 50 3 3 1 > [3] 100 1 1 0 > [4] 100 2 2 0 > > 0->0 > 0.1->1 > 2->2 > 3->3 > 50->4 > 100->5 (X=5) >Does this do what you want? x <- matrix(c(3,100,0.1,3,100,2,5,0,0,50),5) y <- as.data.frame(table(x)) y$Rank <- rank(y$x) m. -- Marianne Promberger Graduate student in Psychology http://www.psych.upenn.edu/~mpromber