[R] is there a way to do dense rank in R

David Winsemius dwinsemius at comcast.net
Fri Jul 2 04:23:30 CEST 2010


On Jul 1, 2010, at 9:30 PM, Suresh Singh wrote:

> I have not been able to find a way to do dense rank in R
>
> Here is an example of what I need
>
> rank() gives the following
>
> 5 rank 1
> 7 rank 2
> 7 rank 2
> 9 *rank 4*
>
> but I want
>
> 5 rank 1
> 7 rank 2
> 7 rank 2
> 9 *rank 3

 > tst <- read.table(textConnection("5 rank 1
+ 7 rank 2
+ 7 rank 2
+ 9 rank 4"), header=FALSE)
 > tst
   V1   V2 V3
1  5 rank  1
2  7 rank  2
3  7 rank  2
4  9 rank  4

# Probably not the most elegant solution.

 > tst$drank <- rep(1:length(rle(tst$V3)$values), rle(tst$V3)$lengths)
 > tst
   V1   V2 V3 drank
1  5 rank  1     1
2  7 rank  2     2
3  7 rank  2     2
4  9 rank  4     3

-- 
David.



More information about the R-help mailing list