sort(),rank()和order()之间的区别 [英] Difference between sort(), rank(), and order()

查看:450
本文介绍了sort(),rank()和order()之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

R中的sort(),rank()和order()之间的差异是什么.

What is the difference between sort(), rank(), and order() in R.

您能举例说明吗?

推荐答案

sort()将向量按升序排序.

rank()给出向量中存在的数字的相应等级,最小的数字获得等级1.

rank() gives the respective rank of the numbers present in the vector, the smallest number receiving the rank 1.

order()以排序的顺序返回向量的索引.

order() returns the indices of the vector in a sorted order.

例如:如果我们将这些函数应用到向量- c(3,1,2,5,4)

for example: if we apply these functions are applied to the vector - c (3, 1, 2, 5, 4)

sort(c (3, 1, 2, 5, 4))将给出c(1,2,3,4,5)

sort(c (3, 1, 2, 5, 4)) will give c(1,2,3,4,5)

rank(c (3, 1, 2, 5, 4))将给出c(3,1,2,5,4)

rank(c (3, 1, 2, 5, 4)) will give c(3,1,2,5,4)

order(c (3, 1, 2, 5, 4))将给出c(2,3,1,5,4). 如果按此顺序放置这些索引,则将获得排序后的向量.请注意v [2] = 1,v [3] = 2,v [1] = 3,v [5] = 4和v [4] = 5

order(c (3, 1, 2, 5, 4)) will give c(2,3,1,5,4). if you put these indices in this order, you will get the sorted vector. Notice how v[2] = 1, v[3] = 2, v[1] = 3, v[5] = 4 and v[4] = 5

R 中还有一种 tie 处理方法.如果您运行rank(c (3, 1, 2, 5, 4, 2)),它将给出1到1的等级,因为当前有两个2,R会将它们分别排在2和3上,但是给它们分别分配等级2.5,那么接下来的3将获得4.0等级,所以

also there is a tie handling method in R. If you run rank(c (3, 1, 2, 5, 4, 2)) it will give Rank 1 to 1, since there are two 2 present R will rank them on 2 and 3 but assign Rank 2.5 to each of them, next 3 will get Rank 4.0, so

rank(c (3, 1, 2, 5, 4, 2))将为您输出 [4.0 1.0 2.5 6.0 5.0 2.5]

希望这会有所帮助.

这篇关于sort(),rank()和order()之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆