Rebecca Kuang
Rebecca Kuang

Reputation: 63

Rank with Ties in R

I have a list of competitors and point values from a tournament. A lot of competitors are tied. I need a way to rank them in R so that, for example, if four competitors tie for fourth place, the output might look like this:

Competitor A: 1 
Competitor B: 2
Competitor C: 3
Competitor D: 4
Competitor E: 4
Competitor F: 4
Competitor G: 4
Competitor F: 8

So the next rank after the tie, instead of being 5, would be 8 (because four competitors took up the slots from 4-7).

I know this is a little counter intuitive, but if anyone has any ideas it would be highly appreciated. Thanks!

data

v1 <- setNames(c(4, 5, 2, 7, 7, 7, 7, 9), paste('Competitor', LETTERS[1:8]))

Upvotes: 4

Views: 3889

Answers (1)

akrun
akrun

Reputation: 887991

We can use min_rank from dplyr

library(dplyr)
min_rank(v1)
#[1] 2 3 1 4 4 4 4 8

Or in base R, (from @Steven Beaupré's comments)

rank(v1, ties.method = "min")

data

v1 <- c(4, 5, 2, 7, 7, 7, 7, 9)

Upvotes: 6

Related Questions