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
as is stated by ?order() in R prompt, order just return a permutation which sort the original vector into ascending/descending order. suppose that we have a vector
then
besides, i find that order has the following property(not validated theoratically):
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 vectorx
in order.The 27th value of
x
is the lowest, so27
is the first element oforder(x)
- and if you look atrank(x)
, the 27th element is1
.I always find it confusing to think about the difference between the two, and I always think, "how can I get to
order
usingrank
"?Starting with Justin's example:
Order using rank:
rank
is more complicated and not neccessarily an index (integer):As it turned out this was a special case and made things confusing. I explain below for anyone interested:
rank
returns the order of each element in an ascending listorder
returns the index each element would have in an ascending listIn layman's language,
order
gives the actual place/position of a value after sorting the values For eg:The position of
1
ina
is 7. similarly position of2
ina
is 3.