[R] A better way to Rank Data that considers "ties"

MRKidd clistkoraelus at yahoo.com
Fri Jan 8 22:09:08 CET 2010


This will start off sounding very easy, but I think it will be very
complicated.

Let's say that I have a matrix, which shows the number of apples that each
person in a group has.


OriginalMatrix<-matrix(c(2,3,5,4,6),nrow=5,ncol=1,byrow=T,dimnames=list(c("Bob","Frank","Joe","Jim","David"),c("Apples")))

        Apples
Bob        2
Frank     3
Joe        5
Jim        4
David     6

I want to add a third column that shows what each person's rank is - e.g.
David is number 1 because he has the most apples. 

So this is what I want:

         Apples    Rank
Bob       2          5
Frank     3          4
Joe       5           2
Jim       4           3
David    6           1

I have managed to do this in the following steps:
Unranked<-rownames(OriginalMatrix)
Ranked<-names(sort(OriginalMatrix,decreasing=T))
Matched<-match(Unranked,Ranked)
NewMatrix<-cbind(OriginalMatrix,Matched)

This is not acceptable, however, if two people have the same number of
apples.

You will get:

NewMatrix

       Apples    Rank
Bob      2          5
Frank   2          4
Joe      5          2
Jim      4          3
David   6          1

Does anyone know of a way to make it so that both Bob and Frank will be
ranked as fourth (i.e.- tied for last place)?
-- 
View this message in context: http://n4.nabble.com/A-better-way-to-Rank-Data-that-considers-ties-tp1009994p1009994.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list