添加按同一列分组的另一列的前一行值(足球数据)

时间:2018-09-03 09:48:59

标签: r data.table

数据

Country League         Season   Month   monthDay    weekDay      Time   HomeTeam    AwayTeam    FTHG    FTAG    FTR HomeTeamPoints  AwayTeamPoints
Denmark Superliga   2015/2016   July          19    Sunday   12:30:00   Odense         Hobro    3       0       H          3               0
Denmark Superliga   2015/2016   July          24    Friday   17:00:00   Hobro        Aalborg    0       1       A          0               3
Denmark Superliga   2015/2016   August         2    Sunday   17:00:00   Brondby        Hobro    0       2       A          0               3
Denmark Superliga   2015/2016   August         8    Saturday 15:00:00   Hobro         Viborg    1       1       D          1               0
Denmark Superliga   2015/2016   August        17    Monday   18:00:00   Sonderjyske    Hobro    3       0       H          0               3
Denmark Superliga   2015/2016   August        21    Friday   17:30:00   Hobro  Nordsjaelland    1       3       A          1               1
Denmark Superliga   2015/2016   August        30    Sunday   19:30:00   Midtjylland    Hobro    2       0       H          7               3

所需的输出:

Country League         Season   Month   monthDay    weekDay      Time   HomeTeam    AwayTeam    FTHG    FTAG    FTR HomeTeamPoints  AwayTeamPoints HomeTeamTotalPoints  AwayTeamTotalPoints
Denmark Superliga   2015/2016   July          19    Sunday   12:30:00   Odense         Hobro    3       0       H          3               0                                   0
Denmark Superliga   2015/2016   July          24    Friday   17:00:00   Hobro        Aalborg    0       1       A          0               3              0
Denmark Superliga   2015/2016   August         2    Sunday   17:00:00   Brondby        Hobro    0       2       A          0               3                                   3  
Denmark Superliga   2015/2016   August         8    Saturday 15:00:00   Hobro         Viborg    1       1       D          1               0              4
Denmark Superliga   2015/2016   August        17    Monday   18:00:00   Sonderjyske    Hobro    3       0       H          0               3                                   4
Denmark Superliga   2015/2016   August        21    Friday   17:30:00   Hobro  Nordsjaelland    1       3       A          1               1              4
Denmark Superliga   2015/2016   August        30    Sunday   19:30:00   Midtjylland    Hobro    2       0       H          7               3                                   4

注意:在前面的示例中,仅显示了与Hobro团队有关的游戏。

我的问题是我想增加每个团队的总积分。在比赛(行)中,我有HomeTeamPoints和AwayTeamPoints。我找不到我可以按HomeTeam分组并添加上次该团队是AwayTeam的积分的方法(反之亦然)。

我设法通过创建两个表并按团队名称进行筛选来获得总分。这为我提供了联赛的当前分类,但并不能帮助我将数据库添加到更多赛季中。

我的目标是每周根据赛季,团队和过去赛季的工作对总积分进行更新。

1 个答案:

答案 0 :(得分:0)

我的建议是使用以下内容,因为它最不混乱,并且可以使您的数据保持整洁:

library(stringr)


League$HomeTeamTotalPoints <- League$HomeTeam == "Hobro"

League$AwayTeamTotalPoints <- League$AwayTeam == "Hobro"

League$HomeTeamTotalPoints <- gsub("FALSE", " ", League$HomeTeamTotalPoints )

League$AwayTeamTotalPoints <- gsub("FALSE", " ", League$AwayTeamTotalPoints )

此后,只需用主队和客队列中各自的数值替换“ TRUE”