在R中,和排序相关的函数主要有三个:sort(),rank(),order()。

   
sort(x)是对向量x进行排序,返回值排序后的数值向量。rank()是求秩的函数,它的返回值是这个向量中对应元素的“排名”。而order()的返回值是对应“排名”的元素所在向量中的位置。

 

> x<-c(30,2,100)
> sort(x)
[1]   2  30 100
> order(x)
[1] 2 1 3
> x[order(x)]
[1]   2  30 100
> rank(x)
[1] 2 1 3
>
> sort(x,decreasing=TRUE)
[1] 100  30   2
> order(x,decreasing=TRUE)
[1] 3 1 2
> x[order(x,decreasing=TRUE)]
[1] 100  30   2
>

posted on 2017-04-20 20:30  guoxiang  阅读(353)  评论(0编辑  收藏  举报