通过两个因素在组内创建 ID

Create ID within a group by two factors

我有以下代码:

set.seed(6)
players<-rep(c("bc","cd","ab","bd"), c(3,3,3,3))
game<-rep(rep(1:2,c(3,3)),2)
my_decs<-(c(0,0,0,0,0,4,0,0,0,0,0,9))
gamematrix<-data.frame(cbind(players,game))

   players game
1       bc    1
2       bc    1
3       bc    1
4       cd    2
5       cd    2
6       cd    2
7       ab    1
8       ab    1
9       ab    1
10      bd    2
11      bd    2
12      bd    2

现在,我想根据这两个因素创建一个新编号:

   players game player_id
1       bc    1         1
2       bc    1         1
3       bc    1         1
7       ab    1         2
8       ab    1         2
9       ab    1         2
4       cd    2         1
5       cd    2         1
6       cd    2         1
10      bd    2         2
11      bd    2         2
12      bd    2         2

也就是说,每场比赛中,球员的编号从1到N

我尝试使用 dplyr:

gamematrix %>% 
      group_by(game) %>%
      mutate(player_id = group_indices(., players)) 

但显然我做错了什么。请帮忙!

按'game'

分组后,我们可以在'players'上使用match
library(dplyr)
gamematrix %>% 
       group_by(game) %>%
       arrange(game) %>% 
       mutate(player_id = match(players, unique(players)))

-输出

# A tibble: 12 x 3
# Groups:   game [2]
#   players game  player_id
#   <chr>   <chr>     <int>
# 1 bc      1             1
# 2 bc      1             1
# 3 bc      1             1
# 4 ab      1             2
# 5 ab      1             2
# 6 ab      1             2
# 7 cd      2             1
# 8 cd      2             1
# 9 cd      2             1
#10 bd      2             2
#11 bd      2             2
#12 bd      2             2

或转换为 factor 并在按 'game' 分组后将 levels 指定为 'players' 的 unique 值,然后强制 factor integeras.integer

gamematrix %>% 
   group_by(game) %>%
   mutate(player_id = as.integer(factor(players, levels = unique(players))))

这可能是另一种选择

transform(
  gamematrix,
  player_id = as.numeric(ave(players,game,FUN = function(x) cumsum(!duplicated(x))))
)

给予

   players game player_id
1       bc    1         1
2       bc    1         1
3       bc    1         1
4       cd    2         1
5       cd    2         1
6       cd    2         1
7       ab    1         2
8       ab    1         2
9       ab    1         2
10      bd    2         2
11      bd    2         2
12      bd    2         2