Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign same value to ties in ranking [duplicate]

Tags:

r

ranking

I have some data that looks like this:

Value <- c(-0.07, -0.07, -0.0003, 0.45, 0.45, 1.2, 1.4, 1.4, 1.4)

I need to convert it to "ranked" data but I would like all ties to be given the same value AND the rankings to be sequential, such that:

# new.value
1 1 2 3 3 4 5 5 5

I've tried the rank() function in R but I'm having trouble with the ties.


1 Answers

You can turn the ranks into a factor, and then convert the factor into integers. Consider:

rank(Value)
# [1] 1.5 1.5 3.0 4.5 4.5 6.0 8.0 8.0 8.0
as.numeric(factor(rank(Value)))
# [1] 1 1 2 3 3 4 5 5 5
like image 153
gung - Reinstate Monica Avatar answered Dec 01 '25 10:12

gung - Reinstate Monica