Hi, I'm wondering if anyone can point me to a function that will allow me to do a ranking that treats ties differently than rank() provides for? I'd like a method that will assign to the elements of each tie group the largest rank. An example: For the vector 'v', I'd like the method to return 'rv' v: 1 2 3 3 3 4 5 5 6 7 rv: 1 2 5 5 5 6 8 8 9 10 Thanks, Doug Grove
Douglas Grove wrote:> Hi, > > I'm wondering if anyone can point me to a function that will > allow me to do a ranking that treats ties differently than > rank() provides for? > > I'd like a method that will assign to the elements of each > tie group the largest rank. > > An example: > > For the vector 'v', I'd like the method to return 'rv' > > v: 1 2 3 3 3 4 5 5 6 7 > rv: 1 2 5 5 5 6 8 8 9 10 > > > Thanks, > Doug Grove >How about rv <- rowSums(outer(v, v, ">=")) Adapted from Prof. Ripley's reply in the following thread: http://finzi.psych.upenn.edu/R/Rhelp02/archive/31993.html HTH, --sundar
Here are a couple of solutions: rx <- rank(x) order(-order(rev(x)))[match(rx,rx)] rx <- rank(x) (2*rx-rank(x,tie="first"))[match(rx,rx)] Douglas Grove <dgrove <at> fhcrc.org> writes: : : Hi, : : I'm wondering if anyone can point me to a function that will : allow me to do a ranking that treats ties differently than : rank() provides for? : : I'd like a method that will assign to the elements of each : tie group the largest rank. : : An example: : : For the vector 'v', I'd like the method to return 'rv' : : v: 1 2 3 3 3 4 5 5 6 7 : rv: 1 2 5 5 5 6 8 8 9 10 : : : Thanks, : Doug Grove
Try:
MxRank <- function(x, na.last = "keep")
{
if (na.last != "keep")
return(rank(x, na.last))
else {
r <- x*NA
NoWarn(r[is.orderable(x)] <- rank(x,na.last=NA))
return(r)
} }
Knut M. Wittkowski, PhD,DSc
------------------------------------------
The Rockefeller University, GCRC
Experimental Design and Biostatistics
1230 York Ave #121B, Box 322, NY,NY 10021
+1(212)327-7175, +1(212)327-8450 (Fax)
kmw at rockefeller.edu
http://www.rucares.org/clinicalresearch/dept/biometry/