# [R] ordering a data.frame by average rank of multiple columns

Gabor Grothendieck ggrothendieck at gmail.com
Fri Aug 10 13:36:11 CEST 2007

```Try this:

positions <- order(ranks)

On 8/10/07, Tom.O <tom.olsson at dnbnor.com> wrote:
>
> Hi
>
> I have run into a problem and i wonder if anyone has a smart way of doing
> this.
>
> For example i have this data frame for 5 different test groups:
>
> Res1 <- c(1,5,4,-0.5,3)
> Res2 <- c(-1,8,2,0,3)
> Mean <- c(0.5,1,1.5,-.5,2)
> MyFrame <- data.frame(Res1,Res2,Mean,row.names=c("G1","G2","G3","G4","G5"))
>
> where the first two columns are the results of two different tests, the
> third column is the mean of the group.
>
> I want to order this data.frame by the combined rank of Res1 & Res2, but
> where weigths are assigned to the importeance av each column. Lets assume
> that Res1 is twice as important and lower values rank better.
>
> MyRanks<-data.frame(Rank1=rank(MyFrame[,"Res1"]),Rank2=rank(MyFrame[,"Res2"]),CombR=2*rank(MyFrame[,"Res1"])+rank(MyFrame[,"Res2"]),row.names=c("G1","G2","G3","G4","G5"))
>
>    Rank1 Rank2 CombR
> G1     2     1     5
> G2     5     5    15
> G3     4     3    11
> G4     1     2     4
> G5     3     4    10
>
>
> and the rank of the combined is 2,5,4,1,3 , but to be able to sort MyFrame
> in that order I need to enter this vector of positions c(4,1,5,3,2) but do
> anyone have a smart way of converting ranks to positions?
>
> Tom
>
>
> --
> View this message in context: http://www.nabble.com/ordering-a-data.frame-by-average-rank-of-multiple-columns-tf4247393.html#a12087498
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help