gpt4 book ai didi

r - 了解 order() 函数

转载 作者:行者123 更新时间:2023-12-03 05:12:10 25 4
gpt4 key购买 nike

我试图了解 order() 函数的工作原理。我的印象是它返回了索引的排列,当排序时,会对原始向量进行排序。

例如,

> a <- c(45,50,10,96)
> order(a)
[1] 3 1 2 4

我预计它会返回 c(2, 3, 1, 4),因为排序后的列表将为 10 45 50 96。

有人可以帮我理解这个函数的返回值吗?

最佳答案

This似乎可以解释一下。

The definition of order is that a[order(a)] is in increasing order. This works with your example, where the correct order is the fourth, second, first, then third element.

You may have been looking for rank, which returns the rank of the elements
R> a <- c(4.1, 3.2, 6.1, 3.1)
R> order(a)
[1] 4 2 1 3
R> rank(a)
[1] 3 2 4 1
so rank tells you what order the numbers are in, order tells you how to get them in ascending order.

plot(a, rank(a)/length(a)) will give a graph of the CDF. To see why order is useful, though, try plot(a, rank(a)/length(a),type="S") which gives a mess, because the data are not in increasing order

If you did
oo<-order(a)
plot(a[oo],rank(a[oo])/length(a),type="S")
or simply
oo<-order(a)
plot(a[oo],(1:length(a))/length(a)),type="S")
you get a line graph of the CDF.

我敢打赌你正在考虑排名。

关于r - 了解 order() 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2315601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com