I have the following code:
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
Now, I want to create a new numbering based on both factors:
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
That is, within each game the players are numbered from 1 to N
I tried to use dplyr:
gamematrix %>%
group_by(game) %>%
mutate(player_id = group_indices(., players))
But apparently I am doing something wrong. Please help!