Confused by rank() and ties

I'd like to rank the vector, x in descending order and find the presence of ties.

x <- c(0, 0, 1, 0, 1, 0, 2, 0)

When I rank() these I get:

> rank(desc(x))
[1] 6.0 6.0 2.5 6.0 2.5 6.0 1.0 6.0
  • The value of 2 gets a 1.0 rank. That makes sense to me.
  • The two values of 1 get a 2.5 rank. That makes sense, too, as the fractional part of the return value signals a tie.
  • The five values of 0 get a 6.0 rank. This confuses me. Both because rank()'s default ties.method is "average" and 6 isn't that, but mostly I'm confused that I get no indication of a tie, despite the five zeros in the vector.

Perhaps I'm going about this all wrong. My ideal output is achieved by rank(x, ties.method = "min") but I also need to know whether ties exist.

Thanks in advance for any guidance.

use table to understand the correspondence between inputs and ranks ?

x <- c(0, 0, 1, 0, 1, 0, 2, 0)
xranked <- rank(x, ties.method = "min") 
table(x,xranked)
# xranked
# x  1 6 8
# 0  5 0 0
# 1  0 2 0
# 2  0 0 1

0 is tied 5 times,
1 tied 2's
2 appears once (no tie)

Thanks for helping me understand that. Much appreciated.

I chose a new, cleaner tack to solve this, writing a short function to return a vector of logicals indicating whether the values are duplicated and thus indicative of a tie. Essentially, just vec %in% unique(vec[duplicated(vec)]).

I then just used rank() with its ties.method = "min" to provide the rank order I required.

I appreciate your help! Thanks again.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.