Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 809
Methexis Avatar asked Oct 17 '14 10:10

Methexis


People also ask

How do you order from largest to smallest in R?

To sort a data frame in R, use the order( ) function. By default, sorting is ASCENDING. Prepend the sorting variable by a minus sign to indicate DESCENDING order.

How do you rank data in R order?

Data Visualization using R Programming The ranking of a variable in an R data frame can be done by using rank function. For example, if we have a data frame df that contains column x then rank of values in x can be found as rank(df$x).

What is rank and order in R?

Data Visualization using R Programming The rank function gives the rank of the values in a vector if the vector is sorted but in the same sequence as the original vector and the order function gives the position of the original value in the vector but in the sequence of the sorting in ascending order.

Is there a rank function in R?

rank() function in R Language is used to return the sample ranks of the values of a vector. Equal values and missing values are handled in multiple ways.


Video Answer


2 Answers

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

rank(-x)
like image 152
Pop Avatar answered Sep 28 '22 05:09

Pop


Or you could use :

> x = c(1,2,3,4,5)
> rank(desc(x))
[1] 5 4 3 2 1
like image 40
Ya Ting Chang Avatar answered Sep 28 '22 05:09

Ya Ting Chang