R - Rank Largest to Smallest

37,035

Solution 1

If you want to get the rank of x from the largest to the smallest, do

rank(-x)

Solution 2

Or you could use :

> x = c(1,2,3,4,5)
> rank(desc(x))
[1] 5 4 3 2 1

Solution 3

Also:

> x = 1:10
> order(-x)
 [1] 10  9  8  7  6  5  4  3  2  1

Solution 4

The following would do it:

order(x, decreasing=TRUE)
Share:
37,035
Methexis
Author by

Methexis

Updated on July 09, 2022

Comments

  • Methexis
    Methexis almost 2 years

    I am using Rank() to assign a rank value to a dataframe, however I need the rank to be 1 = Highest and not 1 = Lowest.

  • Brian
    Brian over 5 years
    I believe order sorts the data, ranking gives a numerical number.
  • Muhammad Dyas Yaskur
    Muhammad Dyas Yaskur about 4 years
    While this code may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion