Reputation: 95
I am currently trying to make a tool that will predict team scores. Essentially, I use take two variables and subtract their corresponding r values. Here is the data frame that I am considering.
Each team A:J has a "r value" associated with it:
# [,1]
#[1,] -0.870 8
#[2,] -0.750 7
#[3,] 2.290 2
#[4,] -0.050 5
#[5,] 0.355 4
#[6,] -0.895 9
#[7,] 3.290 1
#[8,] -0.510 6
#[9,] 0.430 3
#[10,] -3.290 10
I apologize for the lack of clarity, but the "r value" is in column 1, and the value in column 2 corresponds to the letter of the team (i.e. A=1, B=2, etc.)
Referring to the data frame, I am trying to do the following: For the first line, subtract E from A. For the second line, subtract A from F, etc.
I hope this makes sense. I am ultimately unsure on how to go about this in the most effective way possible. Thank you for the help.
Upvotes: 1
Views: 35
Reputation: 887971
We could create a named vector and then use that to match the 'AwayTeam', 'HomeTeam' values and do the subtraction
nm1 <- setNames(m1[,1], LETTERS[m1[,2]])
Reduce(`-`, lapply(df1[c('AwayTeam', 'HomeTeam')], function(x) nm1[x]))
Or it is just
nm1[df1[['AwayTeam']]] - nm1[df1[['HomeTeam']]]
m1 <- structure(c(-0.87, -0.75, 2.29, -0.05, 0.3555, -0.895, 3.29,
-0.51, 0.43, -3.29, 8, 7, 2, 5, 4, 9, 1, 6, 3, 10), .Dim = c(10L,
2L))
df1 <- structure(list(Week = c(3, 3, 4, 4, 5, 5), AwayTeam = c("E",
"A", "H", "I", "F", "F"), HomeTeam = c("A", "F", "E", "A", "C",
"J")), class = "data.frame", row.names = c(NA, -6L))
Upvotes: 2