# [R] rank with ties

Angel angel_lul at hotmail.com
Tue Jul 22 11:41:11 CEST 2003

```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

```