3

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.

gung - Reinstate Monica
  • 11,583
  • 7
  • 60
  • 79

2 Answers2

4

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
gung - Reinstate Monica
  • 11,583
  • 7
  • 60
  • 79
0

In dplyr, you can simply use dense_rank:

dplyr::dense_rank(Value)
# [1] 1 1 2 3 3 4 5 5 5
Maël
  • 45,206
  • 3
  • 29
  • 67