user7919275
user7919275

Reputation:

How the rank function works in R

Comrades! Please teach. How the rank function works in R. I would like to understand exactly the formula and logic by which the function gets the result. For example, independently post it in Excel I don't really understand, for example, how do I get such a result and how to get it myself without using the function.

> rank(c(1,10,1))
[1] 1.5 3.0 1.5

I am sure that there are those who can explain using simple mathematics

Upvotes: 2

Views: 1465

Answers (1)

YevKad
YevKad

Reputation: 680

In your example, there are 3 numbers: 1,10,1

you sort them from smallest to greatest and assign a rank:

  • 1 - rank 1
  • 1 - rank 2
  • 10 - rank 3

but 1 and 1 are the same - so the ties.method is being applied. Default method is "average", therefore ranks 1 and 2 are averaged - (1+2)/2=1.5

if you change your vector and do rank(c(1,10,10)), you'll get 1.0 2.5 2.5:

  • 1 - rank 1
  • 10 - rank 2
  • 10 - rank 3

rank 2 and rank 3 are averaged: (2+3)/2=2.5

you can check the documentation for other ties.method, for instance min would take a smallest rank of duplicate:

rank(c(1,10,1),ties.method='min')
[1] 1 3 1

Upvotes: 8

Related Questions