首页 > 解决方案 > 总结 R 中的足球投注赔率

问题描述

我正在尝试在R中创建一个数据框,该数据框将总结一个足球队基于另一个的投注赔率。

例如,这个非常小的样本表以主队和客队以及各自的比赛赔率为特色。

匹配数据:

关键:HWO(主队获胜赔率)、DO(平局赔率)、AWO(客队获胜赔率)

+----------+----------+------+------+------+
| HomeTeam | AwayTeam | HWO  |  DO  | AWO  |
+----------+----------+------+------+------+
| TeamA    | TeamB    | 1.30 | 5.20 | 9.50 |
| TeamC    | TeamD    | 1.59 | 4.20 | 6.30 |
| TeamE    | TeamF    | 3.00 | 5.50 | 1.70 |
| TeamB    | TeamA    | 1.50 | 4.50 | 8.70 |
| TeamD    | TeamC    | 1.25 | 4.20 | 8.00 |
| TeamF    | TeamE    | 1.40 | 5.00 | 7.20 |
+----------+----------+------+------+------+

这是此数据框的 dput:

structure(list(HomeTeam = c("TeamA", "TeamC", "TeamE", "TeamB", 
"TeamD", "TeamF"), AwayTeam = c("TeamB", "TeamD", "TeamF", "TeamA", 
"TeamC", "TeamE"), HWO = c(1.3, 1.59, 3, 1.5, 1.25, 1.4), DO = c(5.2, 
4.2, 5.5, 4.5, 4.2, 5), AWO = c(9.5, 6.3, 1.7, 8.7, 8, 7.2)), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"))

我需要的额外数据框是每个团队赔率的平均值。它必须考虑该球队是主场还是客场比赛,并为每场比赛使用适当的数字。

下面显示了决赛桌的样子:

赔率总结:

关键字:AvgWO(平均获胜赔率)、AvgDO(平均平局赔率)、AvgLO(平均亏损赔率)

+-------+------+------+------+
| Team  | AvgWO|AvgDO |AvgLO |
+-------+------+------+------+
| TeamA | 5.00 | 4.85 | 5.50 |
| TeamB | 5.50 | 4.85 | 5.00 |
| TeamC | 4.80 | 4.20 | 3.78 |
| TeamD | 3.78 | 4.20 | 4.80 |
| TeamE | 5.10 | 5.25 | 1.55 |
| TeamF | 1.55 | 5.25 | 5.10 |
+-------+------+------+------+

这是此数据框的 dput:

structure(list(Team = c("TeamA", "TeamB", "TeamC", "TeamD", "TeamE", 
"TeamF"), AvgWO = c(5, 5.5, 4.8, 3.78, 5.1, 1.55), AvgDO = c(4.85, 
4.85, 4.2, 4.2, 5.25, 5.25), AvgLO = c(5.5, 5, 7.55, 4.8, 2, 
5.1)), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"
))

比如以 TeamA 为例……</p>

我会感谢任何对此有解决方案的人。请确保它是一个强大的解决方案,可以应对不同数量的游戏等。

标签: raverageframesummary

解决方案


有趣的问题。这是解决方案,它为 TeamC 和 TeamE 的 avgLO 产生不同的答案,但根据您的描述,我认为下面的解决方案是正确的。所以请仔细检查并告诉我。

您可以改进命名等,但我希望这会有所帮助。

DF <-
  data.frame(
    HomeTeam = paste0("Team", c("A", "C", "E", "B", "D", "F")),
    AwayTeam = paste0("Team", c("B", "D", "F", "A", "C", "E")),
    HWO = c(1.3, 1.59, 3, 1.5, 1.25, 1.4),
    DO = c(5.2, 4.2, 5.5, 4.5, 4.2, 5),
    AWO = c(9.5, 6.3, 1.7, 8.7, 8, 7.2)
  )

library(magrittr)
library(dplyr)
library(reshape2)

DF %>%
  melt(c("HWO", "DO", "AWO"), value.name = "Team") %>%
  mutate(WO = ifelse(variable == "HomeTeam", HWO, AWO),
         LO = ifelse(variable == "HomeTeam", AWO, HWO)) %>%
  group_by(Team) %>%
  summarise(avgWO = mean(WO),
            avgDO = mean(DO),
            avgLO = mean(LO))

结果(如果 tibble 不适合您,则转换为 data.frame)

# A tibble: 6 x 4
  Team  avgWO avgDO avgLO
  <chr> <dbl> <dbl> <dbl>
1 TeamA  5     4.85  5.5 
2 TeamB  5.5   4.85  5   
3 TeamC  4.80  4.2   3.78
4 TeamD  3.78  4.2   4.80
5 TeamE  5.1   5.25  1.55
6 TeamF  1.55  5.25  5.1

推荐阅读