Methexis
Methexis

Reputation: 2861

R - Rank Largest to Smallest

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.

Upvotes: 20

Views: 50624

Answers (7)

rnso
rnso

Reputation: 24545

Also, order() can be used with negative sign:

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

Upvotes: 10

Mah
Mah

Reputation: 1

x = c(1,2,3,4,5,6)
x[order(-x)]

you will get

6,5,4,3,2,1

Upvotes: 0

kamiks
kamiks

Reputation: 184

While rank(-x) will certainly work for numeric and logical vectors, it won't work on character vectors, as those cannot be negated. Instead, a solution which work on all types of vectors:

rank(-rank(x))

Upvotes: 3

Ali Raza
Ali Raza

Reputation: 1

ascending_order=arrange[order(arrange)]
Rank_values=rank(ascending_order)

Upvotes: -1

Ya Ting Chang
Ya Ting Chang

Reputation: 149

Or you could use :

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

Upvotes: 12

Ruthger Righart
Ruthger Righart

Reputation: 4921

The following would do it:

order(x, decreasing=TRUE)

Upvotes: 1

Pop
Pop

Reputation: 12411

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

rank(-x)

Upvotes: 46

Related Questions