Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rank and order in R

Tags:

sorting

r

r-faq

i am having trouble understanding the difference between the R function rank and the R function order. they seem to produce the same output:

> rank(c(10,30,20,50,40)) [1] 1 3 2 5 4 > order(c(10,30,20,50,40)) [1] 1 3 2 5 4 

Could somebody shed some light on this for me? Thanks

like image 604
Alex Avatar asked Sep 05 '12 20:09

Alex


People also ask

What is rank and order in R?

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.

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 the difference between sort () rank () and order () function in R?

sort() sorts the vector in an ascending order. rank() gives the respective rank of the numbers present in the vector, the smallest number receiving the rank 1. order() returns the indices of the vector in a sorted order. Hope this is helpful.

What does rank () do 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.


1 Answers

set.seed(1) x <- sample(1:50, 30)     x # [1] 14 19 28 43 10 41 42 29 27  3  9  7 44 15 48 18 25 33 13 34 47 39 49  4 30 46  1 40 20  8 rank(x) # [1]  9 12 16 25  7 23 24 17 15  2  6  4 26 10 29 11 14 19  8 20 28 21 30  3 18 27  1 22 13  5 order(x) # [1] 27 10 24 12 30 11  5 19  1 14 16  2 29 17  9  3  8 25 18 20 22 28  6  7  4 13 26 21 15 23 

rank returns a vector with the "rank" of each value. the number in the first position is the 9th lowest. order returns the indices that would put the initial vector x in order.

The 27th value of x is the lowest, so 27 is the first element of order(x) - and if you look at rank(x), the 27th element is 1.

x[order(x)] # [1]  1  3  4  7  8  9 10 13 14 15 18 19 20 25 27 28 29 30 33 34 39 40 41 42 43 44 46 47 48 49 
like image 135
Justin Avatar answered Oct 06 '22 05:10

Justin