I am working with the R programming language. I created the following "game" in R which involves two players flipping coins (the result of each coin is associated with a "score") to see who gets the highest score:
score_coin_1 = c(-1,1)
score_coin_2 = c(-3, 4)
results <- list()
for (i in 1:100)
{
iteration = i
player_1_coin_choice_i = sample(2, 1, replace = TRUE)
player_2_coin_choice_i = sample(2, 1, replace = TRUE)
player_1_result_i = ifelse(player_1_coin_choice_i == 1, sample(score_coin_1, size=1, prob=c(.5,.5)), sample(score_coin_2, size=1, prob=c(.7,.3)) )
player_2_result_i = ifelse(player_2_coin_choice_i == 1, sample(score_coin_1, size=1, prob=c(.5,.5)), sample(score_coin_2, size=1, prob=c(.7,.3)))
winner_i = ifelse(player_1_result_i > player_2_result_i, "PLAYER_1", ifelse(player_1_result_i == player_2_result_i, "TIE", "PLAYER_2"))
my_data_i = data.frame(iteration, player_1_coin_choice_i, player_2_coin_choice_i, player_1_result_i, player_2_result_i , winner_i )
results[[i]] <- my_data_i
}
results_df <- data.frame(do.call(rbind.data.frame, results))
head(results_df)
iteration player_1_coin_choice_i player_2_coin_choice_i player_1_result_i player_2_result_i winner_i
1 1 1 1 -1 1 PLAYER_2
2 2 1 2 -1 -3 PLAYER_1
3 3 2 2 4 -3 PLAYER_1
4 4 1 2 1 -3 PLAYER_1
5 5 2 1 4 1 PLAYER_1
6 6 2 2 4 -3 PLAYER_1
I then extracted the scores of all possible outcomes:
one_one <- results_df[which(results_df$player_1_coin_choice_i == 1 & results_df$player_2_coin_choice_i == 1), ]
one_two <- results_df[which(results_df$player_1_coin_choice_i == 1 & results_df$player_2_coin_choice_i == 2), ]
two_one <- results_df[which(results_df$player_1_coin_choice_i == 2 & results_df$player_2_coin_choice_i == 1), ]
two_two <- results_df[which(results_df$player_1_coin_choice_i == 2 & results_df$player_2_coin_choice_i == 2), ]
library(dplyr)
one_one_sum = data.frame(one_one %>%
group_by(winner_i) %>%
summarise(n = n()))
one_two_sum = data.frame(one_two %>%
group_by(winner_i) %>%
summarise(n = n()))
two_one_sum = data.frame(two_one %>%
group_by(winner_i) %>%
summarise(n = n()))
two_two_sum = data.frame(two_two %>%
group_by(winner_i) %>%
summarise(n = n()))
From here, I want to make this kind of table ("Prisoner's Dilemma": https://www.greenflux.com/wp-content/uploads/knowledge-sharing-the-prisoners-dilemma-1-1.jpg)
Here, "Coin 1, Coin 1" means Player 1 chose Coin 1 and Player 2 also chose Coin 1 - "Coin 2, Coin 1" means Player 1 chose Coin 2 and Player 1 chose Coin 1, etc.
My Question: Is it possible to "directly" make this table in R? Currently, I am creating this table in Microsoft Excel and Powerpoint - but is possible to make this table directly in R?
Thanks!