首页 > 解决方案 > 在 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

标签: rdata-manipulationrank

解决方案


推荐阅读