r - 在 R 中,使用带有 rank() 的第二列来打破平局
问题描述
编辑:看起来R: Rank-function with two variables and ties.method randomorder(order())
的解决方案仍然有效。
zed = structure(list(sim_id = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L),
group_id = c(225400, 225400, 225400, 225400, 225401, 225401,
225401, 225401, 225402, 225402, 225402, 225402, 225403, 225403,
225403, 225403, 225404, 225404, 225404, 225404, 225405, 225405,
225405, 225405), feed_id = c(18658, 18708, 18721, 18716,
18743, 18570, 18583, 18702, 18694, 18624, 18643, 18689, 18645,
18718, 18588, 18706, 18564, 18710, 18648, 18699, 18660, 18647,
18701, 18732), points = c(9, 4, 4, 0, 9, 3, 3, 3, 9, 6, 3,
0, 5, 5, 4, 1, 7, 5, 3, 1, 6, 5, 4, 1), goal_diff = c(7,
-1, 1, -7, 4, 0, -2, -2, 4, -1, 1, -4, 1, 2, -1, -2, 1, 0,
0, -1, 1, 1, 1, -3)), row.names = c(NA, -24L), class = c("tbl_df",
"tbl", "data.frame"))
> head(zed, 8)
# A tibble: 8 x 5
sim_id group_id feed_id points goal_diff
<int> <dbl> <dbl> <dbl> <dbl>
1 1 225400 18658 9 7
2 1 225400 18708 4 -1
3 1 225400 18721 4 1
4 1 225400 18716 0 -7
5 1 225401 18743 9 4
6 1 225401 18570 3 0
7 1 225401 18583 3 -2
8 1 225401 18702 3 -2
我们有一个大型足球锦标赛的数据,我们需要对 内的球队进行排名group_id
。目前我们的排名如下:
zed %>%
group_by(sim_id, group_id) %>%
mutate(place = rank(-points, ties.method = 'random'))
这会根据 对团队进行排名points
,并随机打破平局。我们想对feed_id
每组内的球队(代表球队)进行排名,首先使用积分,然后通过排名打破平局goal_diff
,然后才随机打破平局。做到这一点的最佳方法是什么,最好是在一个dplyr
链中,但也对其他解决方案开放。
编辑:前 8 行的预期输出为:
> head(zed, 8)
# A tibble: 8 x 5
sim_id group_id feed_id points goal_diff place
<int> <dbl> <dbl> <dbl> <dbl>
1 1 225400 18658 9 7 1
2 1 225400 18708 4 -1 3
3 1 225400 18721 4 1 2
4 1 225400 18716 0 -7 4
5 1 225401 18743 9 4 1
6 1 225401 18570 3 0 2
7 1 225401 18583 3 -2 3or4
8 1 225401 18702 3 -2 3or4
- 在小组
225400
中,goal_diff 打破平局并feed_id == 18721
获得第二名。 - 在小组
225401
中,goal_diff 打破平局并feed_id == 18570
获得第二名。第 3 和第 4 是随机的,因为两者都在点和goal_diff 上并列
解决方案
推荐阅读
- c# - UWP 计算器难度
- arrays - 如何按日期对该数组进行排序?
- flutter - 如何将日期转换为本地日期时间
- swift - 无法将类型“()”的返回表达式转换为返回类型“Double”
- google-sheets - 如何在 FILTER 中为多个条件使用 IF
- sql - SQL 语句从国家列表中的每个国家中选择前 10 个订单
- javascript - 安装组件时自动触发 onClick 中的功能
- javascript - 如何在 .ts 中添加(单击)功能
- amazon-web-services - aws cli缓存位置不正确
- html - 你能把一个html项目转换成reactjs吗?