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

Daniel Malter daniel at umd.edu
Fri Jan 8 22:36:31 CET 2010


Type

?rank

in the prompt and look at the ties.method argument.

Best,
Daniel
-------------------------
cuncta stricte discussurus
-------------------------
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of MRKidd
Sent: Friday, January 08, 2010 4:09 PM
To: r-help at r-project.org
Subject: [R] A better way to Rank Data that considers "ties"


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("B
ob","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-tp1009994
p1009994.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
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.



More information about the R-help mailing list