As said in my title, I’ve got a database of fighters’ statistics where the names alternate. My database illustrates what I mean :
Database <- c("Fighter A", "Fighter B", "Fighter A", "Fighter B",
"Fighter A", "Fighter B", "Fighter A", "Fighter B",
"Fighter C", "Fighter D", "Fighter C", "Fighter D",
"Fighter A", "Fighter C")
I want to add a column based on the observations in the fighters’ names column. In my example above I would like a round column built as follows:
Database <- mutate(round = c(0, 0, 1, 1, 2, 2, 3, 3, 0, 0, 1, 1, 0, 0)
If the pair (fighter A and fighter B for example) is repeated, the round will receive 0, 0, 1, 1 as many times as the pattern is repeated. If the pattern is broken (fighter C and fighter D) then the round restarts at 0. It’s a way to count the number of rounds where round = 0 is the average of the following rounds. In all cases, the round will always have 0, 0, 1, 1 values.
Here’s what I tried without success:
fighters_name <- c("Combattant A", "Combattant B", "Combattant A", "Combattant B",
"Combattant A", "Combattant B", "Combattant A", "Combattant B",
"Combattant C", "Combattant D", "Combattant C", "Combattant D",
"Combattant A", "Combattant C")
df <- tibble(fighters_name)
df <- df %>%
mutate(pair_id = cumsum(fighters_name != lag(fighters_name, default = fighters_name[1]))) %>%
group_by(pair_id) %>%
mutate(round = (row_number() - 1) %% 2) %>%
ungroup() %>%
select(-pair_id)
If someone has an idea to build the round column I would be grateful. Thank you.