我在 R 中有很多关于运动队及其比赛首发阵容的数据。我的数据集示例如下:
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))
我想要实现的是在每个比赛日为每个“球员对球员”比赛创建一个单独的条目。我希望我的输出看起来像:
output <- data.frame(match_id = 1, player_name = "andrew", played_against = c("dan",
"john", "phil", "matthew", "simon"), score_for = 2, score_against = 1)
因此,我可以在一对一的基础上分析和比较表现,而不是每个球员在那天与每支球队比赛。
编辑:我只想将球员与对手球队的球员进行比较。此外,我只需要将球员与他们在 MATCH_ID 上面对的球队的球员进行比较。所以,在这个例子中,每个球员将有 5 行条目(1 代表他们在特定比赛中对阵的球队中的每个球员)
任何人都可以帮助我实现这一目标的最佳方法吗?我有一些使用重塑或融化的经验,但在这种情况下无法让它产生我想要的东西。
任何人都可以推荐实现我需要的最佳方法吗?