Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么顺序(order(x))等于R中的秩(x)?_R_Sorting_Rank - Fatal编程技术网

为什么顺序(order(x))等于R中的秩(x)?

为什么顺序(order(x))等于R中的秩(x)?,r,sorting,rank,R,Sorting,Rank,在此说明,order(order(x))与rank(x)相同 虽然一些实验证实了这一点 #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 为什么顺序(order(x))==rank(x)? x首先,看看由1:10排列形成的整数序列会发生什么

在此说明,
order(order(x))
rank(x)
相同

虽然一些实验证实了这一点

#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
为什么顺序(order(x))==rank(x)?
x首先,看看由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
>set.seed(123);x x
[1]  3  8  4  7  6  1 10  9  2  5
>订单(x)
[1]  6  9  1  3 10  5  4  2  8  7
>订单(订单(x))
[1]  3  8  4  7  6  1 10  9  2  5
>排名(x)
[1]  3  8  4  7  6  1 10  9  2  5
在这种情况下,
顺序
-操作本身是相反的。由于
order
-操作总是返回一个从1开始的序列,因此
order
的任何嵌套奇数应用程序都将给出相同的向量


Order返回可用于对原始向量排序的索引向量。所以最小项的位置在第一个位置,第二个最小值的位置在下一个位置,最后一项是最大项的位置。因此,当您再次对索引向量执行该操作时,第一项现在是最小索引的索引,依此类推。。。。向量的秩。

我认为直觉是这样的:秩只是一个快捷方式,表示“如果首先对所有项目进行排序,那么第一、第二……最后一个位置的项目将位于哪个位置”

因为我们讨论的是向量的位置,所以我们必须嵌套
order
函数

如上例所示,从第一个位置的外部
order
函数开始,这个函数告诉我们最小索引(=2)的位置。现在,最小的索引恰好位于如果您对其进行排序的数字所在的位置(这是
order
函数所做的),因此在上面的示例中,因为第一个数字(=0.2)是第二大数字,所以它位于第二位


因此,这只是一种花哨的说法,如果你先对向量排序,那么第一个数字将排在第二位,这是
rank
函数给出的其他功能所没有的。

使用
ties.method=“first”
rank
实际上使用了
sort.list(sort.list(x))
@RomanCheplyaka:这可能是你的问题?