在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
>

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-09-10
  • 2022-12-23
  • 2021-08-30
  • 2021-12-28
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-02-11
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案