[R] rank with ties

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Jul 22 11:56:26 CEST 2003

```sort.list(sort.list(x)) does ranks, with first occurrence winning (which
is what sort does, BTW).

If you really want ties broken at random you could do something like

sort.list(order(x, runif(length(x)))

On Tue, 22 Jul 2003, Angel wrote:

> Hi,
> Is there a function like rank but that solves the ties by randomly assigning
> a value (doesn't average ranks of ties).
> This is what I actually need:
> I want to make NA all elements of each column in an array that are ranked in
> a position larger that rankmax for each column.
> # Say I've got an array b:
> b<-cbind(c(1:5,5:1),c(1,12,14,2,5,4:8))
> #> b
>  #     [,1] [,2]
>  #[1,]    1    1
>  #[2,]    2   12
>  #[3,]    3   14
>  #[4,]    4    2
>  #[5,]    5    5
>  #[6,]    5    4
>  #[7,]    4    5
>  #[8,]    3    6
>  #[9,]    2    7
> #[10,]    1    8
>
> rankmax<-5   #  The maximum rank position
>
> # I make the values ranked in a position larger than 5 NAs
> b[which(apply(b,2,rank)>rankmax)]<-NA
>
> # > b
> #      [,1] [,2]
> # [1,]    1    1
> # [2,]    2   NA
> # [3,]   NA   NA
> # [4,]   NA    2
> # [5,]   NA    5
> # [6,]   NA    4
> # [7,]   NA    5
> # [8,]   NA   NA
> # [9,]    2   NA
> #[10,]    1   NA
>
> ###  What I want is one of the "3" in the first column not to be made NA.
> (similar to the algorithm used in sort)
>
>
> Thanks in advance for any help,
> Angel
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>

--
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

```