R中的排名和顺序 [英] rank and order in R

查看:26
本文介绍了R中的排名和顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解 R 函数 rank 和 R 函数order 之间的区别.它们似乎产生相同的输出:

i am having trouble understanding the difference between the R function rank and the R function order. they seem to produce the same output:

> rank(c(10,30,20,50,40))
[1] 1 3 2 5 4
> order(c(10,30,20,50,40))
[1] 1 3 2 5 4

有人可以为我解释一下吗?谢谢

Could somebody shed some light on this for me? Thanks

推荐答案

set.seed(1)
x <- sample(1:50, 30)    
x
# [1] 14 19 28 43 10 41 42 29 27  3  9  7 44 15 48 18 25 33 13 34 47 39 49  4 30 46  1 40 20  8
rank(x)
# [1]  9 12 16 25  7 23 24 17 15  2  6  4 26 10 29 11 14 19  8 20 28 21 30  3 18 27  1 22 13  5
order(x)
# [1] 27 10 24 12 30 11  5 19  1 14 16  2 29 17  9  3  8 25 18 20 22 28  6  7  4 13 26 21 15 23

rank 返回一个带有rank"的向量.每个值.第一个位置的数字是第 9 个最低的.order 返回将初始向量 x 排序的索引.

rank returns a vector with the "rank" of each value. the number in the first position is the 9th lowest. order returns the indices that would put the initial vector x in order.

x 的第 27 个值是最低的,所以 27order(x) 的第一个元素 - 如果你看rank(x),第27个元素是1.

The 27th value of x is the lowest, so 27 is the first element of order(x) - and if you look at rank(x), the 27th element is 1.

x[order(x)]
# [1]  1  3  4  7  8  9 10 13 14 15 18 19 20 25 27 28 29 30 33 34 39 40 41 42 43 44 46 47 48 49

这篇关于R中的排名和顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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