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.
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.
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).
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.
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.
If you want to get the rank of x
from the largest to the smallest, do
rank(-x)
Or you could use :
> x = c(1,2,3,4,5)
> rank(desc(x))
[1] 5 4 3 2 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With