足球R编程中的H2H比赛

时间:2018-08-31 08:25:38

标签: r

是否有可能不必写足球队的名称来观看H2H?

我有这些原始变量:

Country    League    Season     Month monthDay  weekDay  Time  HomeTeam  AwayTeam  
Denmark    Superliga 2012/2013  July        13  Friday   17:30 Aarhus    Aalborg

并创建新的变量来做

datafootball <-datafootball[, H2H := as.factor(paste0(HomeTeam,"-", AwayTeam)) ]  
datafootball <-datafootball[, H2Hinverse := as.factor(paste0(AwayTeam,"-", HomeTeam)) ].

新变量:

H2H            H2Hinverse
Aarhus-Aalborg Aalborg-Aarhus

如果我想检查团队(所有行)之间的H2H,可以通过写下相关团队的名称来完成:

 datafootball[datafootball$H2H == "Aalborg-Aarhus" | datafootball$H2H == "Aarhus-Aalborg" , ]

然后,我再次询问:

是否有可能不必写足球队的名称来观看H2H?也许是一个功能。也许使用HomeTeam和AwayTeam代替H2H和H2Hinverse。

条件:

基于即将进行的比赛。也就是说,如果我们假设下一场比赛将是巴塞罗那皇家马德里:

HomeTeam   AwayTeam  
Barcelona  Real Madrid

我想展示H2H在家里玩还是作为访客玩。但是,由于每周都有很多比赛,也有几个联赛,我不想写球队的名称并逐一参赛。

很抱歉问我,但我不是专业程序员,所以我什么也没想到。

非常感谢您。

1 个答案:

答案 0 :(得分:0)

不需要H2H和H2H_inverse(我认为)... 我为您的数据添加了一些行,以进行测试。

library(data.table)

datafootball  <- read.table(text="Country    League    Season     Month monthDay  weekDay  Time  HomeTeam  AwayTeam  
Denmark    Superliga 2012/2013  July        13  Friday   17:30 Aarhus    Aalborg
                            Denmark    Superliga 2012/2013  July        13  Friday   17:30 Aarhus    Barcelona
                            Denmark    Superliga 2012/2013  July        13  Friday   17:30 Aalborg    Aarhus", header = TRUE) %>% setDT

datafootball
#    Country    League    Season Month monthDay weekDay  Time HomeTeam  AwayTeam
# 1: Denmark Superliga 2012/2013  July       13  Friday 17:30   Aarhus   Aalborg
# 2: Denmark Superliga 2012/2013  July       13  Friday 17:30   Aarhus Barcelona
# 3: Denmark Superliga 2012/2013  July       13  Friday 17:30  Aalborg    Aarhus

#give name of teams you want to see the H2H-matches of
teams <- c("Aalborg", "Aarhus")

#return all matches where both teams are playing
datafootball[HomeTeam %in% teams & AwayTeam %in% teams,]

#    Country    League    Season Month monthDay weekDay  Time HomeTeam AwayTeam
# 1: Denmark Superliga 2012/2013  July       13  Friday 17:30   Aarhus  Aalborg
# 2: Denmark Superliga 2012/2013  July       13  Friday 17:30  Aalborg   Aarhus