如何在R中制作排名列
我有一个包含 M1
、M2
和 M3
列的数据库。这些M值对应于通过每种方法获得的值。我现在的想法是为他们每个人制作一个排名列。对于M1
和M2
,排名将从最高值到最低值,而M3
则相反。我做了一个输出表给你看。
df1<-structure(list(M1 = c(400,300, 200, 50), M2 = c(500,200, 10, 100), M3 = c(420,330, 230, 51)), class = "data.frame", row.names = c(NA,-4L))
> df1
M1 M2 M3
1 400 500 420
2 300 200 330
3 200 10 230
4 50 100 51
输出
> df1
M1 rank M2 rank M3 rank
1 400 1 500 1 420 4
2 300 2 200 2 330 3
3 200 3 10 4 230 2
4 50 4 100 3 51 1
调整排名:
I have a database with columns M1
, M2
and M3
. These M values correspond to the values obtained by each method. My idea is now to make a rank column for each of them. For M1
and M2
, the rank will be from the highest value to the lowest value and M3
in reverse. I made the output table for you to see.
df1<-structure(list(M1 = c(400,300, 200, 50), M2 = c(500,200, 10, 100), M3 = c(420,330, 230, 51)), class = "data.frame", row.names = c(NA,-4L))
> df1
M1 M2 M3
1 400 500 420
2 300 200 330
3 200 10 230
4 50 100 51
Output
> df1
M1 rank M2 rank M3 rank
1 400 1 500 1 420 4
2 300 2 200 2 330 3
3 200 3 10 4 230 2
4 50 4 100 3 51 1
Adjust rankings:
I used the code, but in a case I'm working on, my rankings looked like this:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
rank
和relocate
:如果向量中有重复值,则必须选择一种关系方法。默认情况下,您会获得平均排名,但您可以选择“第一”。
另一种可能性,我认为你想要做的,是转换为因子,然后转换为数字,这样你就只能得到整个值(而不是平均值)。
Using
rank
andrelocate
:If you have duplicate values in your vector, then you have to choose a method for ties. By default, you get the average rank, but you can choose "first".
Another possibility, which is I think what you want to do, is to convert to factor and then to numeric, so that you get a only entire values (not the average).
为了实现这个结果,我要做的是将每个 df 列转换为因子,然后再次将它们转换为数字。这可以在基本的 R 中完成,但为了简单起见,我报告了 tidyverse 代码:
What I would do to achieve this result would be to transform each of the df columns into factors, and then convert them into numeric again. This could be accomplished in basic R, but for simplicity I report the tidyverse code:
另一种可能的解决方案基于 dplyr:
Another possible solution, based on
dplyr
: