c*******h 发帖数: 51 | 1 What is the difference between rank and order? In the following examples,
the first line of order function makes sure to me. But I don't understand
the second line of order function. Why change of the first two numbers will
affect the third number's order although the third number does not change
position at all?
> order(c(3,2.1,1,4))
[1] 3 2 1 4
> order(c(2.1,3,1,4))
[1] 3 1 2 4 | c*****m 发帖数: 4817 | 2 rank返回的是这四个数字从小到大的排名,
而order返回的是这四个数字从小到大的位置,
比如你第一个例子得到的3214表示最小的数字是第3个,
第二小的数字是第2个,最大的数字是第四个。
所以a[order(a)] = sort(a), 返回的是sorted a | c*******h 发帖数: 51 | 3 多谢了! 真是高手。所以rank不能用来sort.
【在 c*****m 的大作中提到】 : rank返回的是这四个数字从小到大的排名, : 而order返回的是这四个数字从小到大的位置, : 比如你第一个例子得到的3214表示最小的数字是第3个, : 第二小的数字是第2个,最大的数字是第四个。 : 所以a[order(a)] = sort(a), 返回的是sorted a
| s******h 发帖数: 539 | 4 我刚想了下,rank也可以用来sort的,比如 a[rank(a)] = a, 这样子a就被sort了。
或者你不想改变a, 随便搞个跟a长度一样的b, say b = rep(0, length(a)), 然后 b[
rank(a)] = a
【在 c*******h 的大作中提到】 : 多谢了! 真是高手。所以rank不能用来sort.
| u**r 发帖数: 160 | 5 This may be useful.
http://www.portfolioprobe.com/2012/07/26/r-inferno-ism-order-is
Take home message: "The command order(order(x)) is another way to get what
our rank command gives us. Even though it is a bit kludgy, it can be
significantly faster" |
|