Reputation: 1
I have a table with "power" values in one column and team ids in the other. There is a function I have to calculate the probability team A beats team B. I want to make a matrix in R that performs this function on every combination of teams. Something that would have the team ids as the rows and columns and where they meet is the probability the team id in the row would beat the team in the column. I am fairly new to R so I'm not quite sure how to go about this.
Upvotes: 0
Views: 37
Reputation: 17195
In what may be the ugliest possible answer to this question, a brut-force way to achieve this is below. I broke it out step by step. Hopefully a more elegant coder than me can improve upon it. Since there is no sample data I am not sure if this is exactly what you need.
set.seed(05062020)
# sample data
df <- data.frame(team = LETTERS[1:10],
power = runif(10))
# team power
# 1 A 0.06839351
# 2 B 0.99013777
# 3 C 0.65360185
# 4 D 0.87851168
# ....
# 8 H 0.83947190
# 9 I 0.17248571
# 10 J 0.21813885
# all possible combination of teams
df2 <- data.frame(expand.grid(df$team, df$team))
colnames(df2) <- c("team", "team2")
# > df2
# team team2
# 1 A A
# 2 B A
# 3 C A
#.....
# 98 H J
# 99 I J
# 100 J J
## add in power values
df3 <- merge(df2, df, by = "team")
colnames(df3) <- c("team1", "team", "power1")
df4 <- merge(df3, df, by = "team")
df4 <- df4[,c(2:1,3:4)] #rearrange
colnames(df4) <- c("team1", "team2", "power1", "power2")
# Define whatever function you want to use, this is just a dummy function
test_fun <- function(team1, team2, p1, p2){
if(p1 == p2) {NA}
else {
if(p1>p2){paste0("Team ", team1, " wins")}
else{paste0("Team ", team2, " wins")}
}
}
# apply across rows
df4$winner <- apply(df4, 1, function(x) test_fun(x[1], x[2], x[3], x[4]))
# team1 team2 power1 power2 winner
#1 A A 0.06839351 0.06839351 <NA>
#2 B A 0.99013777 0.06839351 Team B wins
#3 C A 0.65360185 0.06839351 Team C wins
#4 H A 0.83947190 0.06839351 Team H wins
#5 D A 0.87851168 0.06839351 Team D wins
# ......
#97 C J 0.65360185 0.21813885 Team C wins
#98 H J 0.83947190 0.21813885 Team H wins
#99 I J 0.17248571 0.21813885 Team J wins
#100 J J 0.21813885 0.21813885 <NA>
Upvotes: 1