Here is my R code about a coin flipping game in which players keep flipping coins until their first tails (and then they are eliminated):
num_players <- 3
results <- data.frame(player = paste0("player", 1:num_players))
status <- rep("playing", num_players)
seen_tails <- rep(FALSE, num_players)
round <- 1
# simulation
while (any(seen_tails == FALSE)) {
coin_flips <- ifelse(runif(num_players) > 0.5, "heads", "tails")
seen_tails <- ifelse(coin_flips == "tails", TRUE, seen_tails)
current_round <- ifelse(status == "playing", coin_flips, "eliminated")
status <- ifelse(seen_tails == TRUE, "eliminated", status)
results <- cbind(results, current_round)
colnames(results)[ncol(results)] <- paste0("round_", round)
round <- round + 1
}
num_tails_last_round <- sum(results[,ncol(results)] == "tails")
The results look like this:
> results
player round_1 round_2 round_3
1 player1 heads heads tails
2 player2 tails eliminated eliminated
3 player3 heads heads tails
> num_tails_last_round
[1] 2
I would like to modify my R code so that in the above case, an extra column would be added for round_4 in which all 3 players would are eliminated:
> results
player round_1 round_2 round_3 round 4
1 player1 heads heads tails eliminated
2 player2 tails eliminated eliminated eliminated
3 player3 heads heads tails eliminated
If the results of this simulation had looked like this, then I want to add a winner row is added:
> results
player round_1 round_2 round_3 round 4
1 player1 heads heads tails winner
2 player2 tails eliminated eliminated eliminated
3 player3 heads tails tails eliminated
How can I do this?