I have a lot of data in R for sports teams and their starting line-ups for matches. An example of my dataset is below:
matchdata <- data.frame(match_id = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2), player_name = c("andrew", "david", "james", "steve", "tim", "dan",
"john", "phil", "matthew", "simon", "ben", "brian", "evan", "tony", "will",
"alister", "archie", "paul", "peter", "warren"), played_for = c("team a", "team a",
"team a", "team a", "team a", "team b", "team b", "team b", "team b", "team b",
"team c", "team c", "team c", "team c", "team c", "team d", "team d", "team d",
"team d", "team d"), played_against = c("team b", "team b", "team b", "team b",
"team b", "team a", "team a", "team a", "team a", "team a", "team d", "team d",
"team d", "team d", "team d", "team c", "team c", "team c", "team c", "team c"),
score_for = c(2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0),
score_against = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3))
What I am trying to achieve is to create a separate entry for each 'player vs player' match-up on each matchday. I want my output to look something like:
output <- data.frame(match_id = 1, player_name = "andrew", played_against = c("dan",
"john", "phil", "matthew", "simon"), score_for = 2, score_against = 1)
So, rather than each player playing against each team on that day, I can analyse and compare performances on a one-v-one basis.
EDIT: I only want to compare players with the players on the OPPOSING team. Also I only need to compare players with players from the team they faced ON THAT MATCH_ID. So, in this example, each player would have 5 lines of entries (1 for each player on the team that they played AGAINST in that particular matchup)
Can anyone help me with the best way to achieve this please? I have some experience using reshape or melt but cannot get it to produce what I want in this instance.
Can anyone recommend the best way to achieve what I need please?