问题
In this post it is stated that order(order(x))
is the same as rank(X)
.
While some experiments corroborate this...
#why is order(order(x)) == rank(x)?
x <- c(0.2, 0.5, 0.1)
x
## [1] 0.2 0.5 0.1
order(x)
## [1] 3 1 2
rank(x)
## [1] 2 3 1
order(order(x))
## [1] 2 3 1
I fail to see how this can be proved and, even better, intuited.
Related: rank and order in R
回答1:
First off look at what happens with an integer sequence formed from a permutation of 1:10:
> set.seed(123); x <- sample(10)
> x
[1] 3 8 4 7 6 1 10 9 2 5
> order(x)
[1] 6 9 1 3 10 5 4 2 8 7
> order(order(x))
[1] 3 8 4 7 6 1 10 9 2 5
> rank(x)
[1] 3 8 4 7 6 1 10 9 2 5
The order
-operation is its own inverse in this case. And since the order
-operation always returns an sequence starting at 1, then any nested odd applications of order
will be giving the same vector.
Order returns an index vector that can be used to sort the original vector. So the location of the smallest item is in the first position, the location of the second smallest value is next, .... , and the last item is the location of the largest item. So when you then again perform that operation on the index vector, the first item is now the index of the smallest index, and so on .... and that the vector's rank.
回答2:
I think the intuition is something along those lines: rank is just a shortcut for "on which position would the item on the first, second, ... last position be if one sorted all items first".
Because we are talking about the position of a position of a vector we have to nest the order
function.
As in the example above starting from the outer order
function in the first position this one tells us where the smallest index is (= 2). Now the smallest index happens to be on the position where the number would be if you ordered it (this is what the order
function does), so in the example above because the first number (= 0.2) is the second largest one it is on position number two.
So this is just a fancy way of saying that the first number would be on second place if you ordered the vector first - which is nothing else what the rank
function gives.
来源:https://stackoverflow.com/questions/38968911/why-does-orderorderx-is-equal-to-rankx-in-r