Import data

# excel file
data <- read_excel("../00_data/mydata.xlsx")
data
## # A tibble: 380 × 22
##    Date      HomeTeam AwayTeam  FTHG  FTAG FTR    HTHG  HTAG HTR   Referee    HS
##    <chr>     <chr>    <chr>    <dbl> <dbl> <chr> <dbl> <dbl> <chr> <chr>   <dbl>
##  1 13/08/20… Brentfo… Arsenal      2     0 H         1     0 H     M Oliv…     8
##  2 14/08/20… Man Uni… Leeds        5     1 H         1     0 H     P Tier…    16
##  3 14/08/20… Burnley  Brighton     1     2 A         1     0 H     D Coote    14
##  4 14/08/20… Chelsea  Crystal…     3     0 H         2     0 H     J Moss     13
##  5 14/08/20… Everton  Southam…     3     1 H         0     1 A     A Madl…    14
##  6 14/08/20… Leicest… Wolves       1     0 H         1     0 H     C Paws…     9
##  7 14/08/20… Watford  Aston V…     3     2 H         2     0 H     M Dean     13
##  8 14/08/20… Norwich  Liverpo…     0     3 A         0     1 A     A Marr…    14
##  9 15/08/20… Newcast… West Ham     2     4 A         2     1 H     M Atki…    17
## 10 15/08/20… Tottenh… Man City     1     0 H         0     0 D     A Tayl…    13
## # ℹ 370 more rows
## # ℹ 11 more variables: AS <dbl>, HST <dbl>, AST <dbl>, HF <dbl>, AF <dbl>,
## #   HC <dbl>, AC <dbl>, HY <dbl>, AY <dbl>, HR <dbl>, AR <dbl>

Apply the following dplyr verbs to your data

Filter rows

filter(data, HomeTeam == "Liverpool", FTHG > 2)
## # A tibble: 9 × 22
##   Date       HomeTeam AwayTeam  FTHG  FTAG FTR    HTHG  HTAG HTR   Referee    HS
##   <chr>      <chr>    <chr>    <dbl> <dbl> <chr> <dbl> <dbl> <chr> <chr>   <dbl>
## 1 18/09/2021 Liverpo… Crystal…     3     0 H         1     0 H     A Madl…    25
## 2 20/11/2021 Liverpo… Arsenal      4     0 H         1     0 H     M Oliv…    19
## 3 27/11/2021 Liverpo… Southam…     4     0 H         3     0 H     A Marr…    20
## 4 16/12/2021 Liverpo… Newcast…     3     1 H         2     1 H     M Dean     23
## 5 16/01/2022 Liverpo… Brentfo…     3     0 H         1     0 H     J Moss     27
## 6 19/02/2022 Liverpo… Norwich      3     1 H         0     0 D     M Dean     29
## 7 23/02/2022 Liverpo… Leeds        6     0 H         3     0 H     M Oliv…    23
## 8 19/04/2022 Liverpo… Man Uni…     4     0 H         2     0 H     M Atki…    14
## 9 22/05/2022 Liverpo… Wolves       3     1 H         1     1 D     A Tayl…    29
## # ℹ 11 more variables: AS <dbl>, HST <dbl>, AST <dbl>, HF <dbl>, AF <dbl>,
## #   HC <dbl>, AC <dbl>, HY <dbl>, AY <dbl>, HR <dbl>, AR <dbl>
filter(data, HomeTeam == "Liverpool" & FTHG > 2)
## # A tibble: 9 × 22
##   Date       HomeTeam AwayTeam  FTHG  FTAG FTR    HTHG  HTAG HTR   Referee    HS
##   <chr>      <chr>    <chr>    <dbl> <dbl> <chr> <dbl> <dbl> <chr> <chr>   <dbl>
## 1 18/09/2021 Liverpo… Crystal…     3     0 H         1     0 H     A Madl…    25
## 2 20/11/2021 Liverpo… Arsenal      4     0 H         1     0 H     M Oliv…    19
## 3 27/11/2021 Liverpo… Southam…     4     0 H         3     0 H     A Marr…    20
## 4 16/12/2021 Liverpo… Newcast…     3     1 H         2     1 H     M Dean     23
## 5 16/01/2022 Liverpo… Brentfo…     3     0 H         1     0 H     J Moss     27
## 6 19/02/2022 Liverpo… Norwich      3     1 H         0     0 D     M Dean     29
## 7 23/02/2022 Liverpo… Leeds        6     0 H         3     0 H     M Oliv…    23
## 8 19/04/2022 Liverpo… Man Uni…     4     0 H         2     0 H     M Atki…    14
## 9 22/05/2022 Liverpo… Wolves       3     1 H         1     1 D     A Tayl…    29
## # ℹ 11 more variables: AS <dbl>, HST <dbl>, AST <dbl>, HF <dbl>, AF <dbl>,
## #   HC <dbl>, AC <dbl>, HY <dbl>, AY <dbl>, HR <dbl>, AR <dbl>
filter(data, HomeTeam == "Liverpool" | AwayTeam == "Liverpool")
## # A tibble: 38 × 22
##    Date      HomeTeam AwayTeam  FTHG  FTAG FTR    HTHG  HTAG HTR   Referee    HS
##    <chr>     <chr>    <chr>    <dbl> <dbl> <chr> <dbl> <dbl> <chr> <chr>   <dbl>
##  1 14/08/20… Norwich  Liverpo…     0     3 A         0     1 A     A Marr…    14
##  2 21/08/20… Liverpo… Burnley      2     0 H         1     0 H     M Dean     27
##  3 28/08/20… Liverpo… Chelsea      1     1 D         1     1 D     A Tayl…    24
##  4 44539     Leeds    Liverpo…     0     3 A         0     1 A     C Paws…     9
##  5 18/09/20… Liverpo… Crystal…     3     0 H         1     0 H     A Madl…    25
##  6 25/09/20… Brentfo… Liverpo…     3     3 D         1     1 D     S Attw…    12
##  7 44265     Liverpo… Man City     2     2 D         0     0 D     P Tier…     6
##  8 16/10/20… Watford  Liverpo…     0     5 A         0     2 A     J Moss      6
##  9 24/10/20… Man Uni… Liverpo…     0     5 A         0     4 A     A Tayl…    12
## 10 30/10/20… Liverpo… Brighton     2     2 D         2     1 H     M Dean     14
## # ℹ 28 more rows
## # ℹ 11 more variables: AS <dbl>, HST <dbl>, AST <dbl>, HF <dbl>, AF <dbl>,
## #   HC <dbl>, AC <dbl>, HY <dbl>, AY <dbl>, HR <dbl>, AR <dbl>
filter(data, Referee %in% c("M Oliver", "P Tierney"))
## # A tibble: 53 × 22
##    Date      HomeTeam AwayTeam  FTHG  FTAG FTR    HTHG  HTAG HTR   Referee    HS
##    <chr>     <chr>    <chr>    <dbl> <dbl> <chr> <dbl> <dbl> <chr> <chr>   <dbl>
##  1 13/08/20… Brentfo… Arsenal      2     0 H         1     0 H     M Oliv…     8
##  2 14/08/20… Man Uni… Leeds        5     1 H         1     0 H     P Tier…    16
##  3 22/08/20… Arsenal  Chelsea      0     2 A         0     2 A     P Tier…     6
##  4 23/08/20… West Ham Leicest…     4     1 H         1     0 H     M Oliv…    13
##  5 28/08/20… Newcast… Southam…     2     2 D         0     0 D     P Tier…    10
##  6 29/08/20… Burnley  Leeds        1     1 D         0     0 D     M Oliv…    12
##  7 44509     Arsenal  Norwich      1     0 H         0     0 D     M Oliv…    30
##  8 44509     Leicest… Man City     0     1 A         0     0 D     P Tier…     6
##  9 19/09/20… Tottenh… Chelsea      0     3 A         0     0 D     P Tier…     8
## 10 25/09/20… Chelsea  Man City     0     1 A         0     0 D     M Oliv…     5
## # ℹ 43 more rows
## # ℹ 11 more variables: AS <dbl>, HST <dbl>, AST <dbl>, HF <dbl>, AF <dbl>,
## #   HC <dbl>, AC <dbl>, HY <dbl>, AY <dbl>, HR <dbl>, AR <dbl>
# Remove missing values
data %>% 
    filter(!is.na(Referee))
## # A tibble: 380 × 22
##    Date      HomeTeam AwayTeam  FTHG  FTAG FTR    HTHG  HTAG HTR   Referee    HS
##    <chr>     <chr>    <chr>    <dbl> <dbl> <chr> <dbl> <dbl> <chr> <chr>   <dbl>
##  1 13/08/20… Brentfo… Arsenal      2     0 H         1     0 H     M Oliv…     8
##  2 14/08/20… Man Uni… Leeds        5     1 H         1     0 H     P Tier…    16
##  3 14/08/20… Burnley  Brighton     1     2 A         1     0 H     D Coote    14
##  4 14/08/20… Chelsea  Crystal…     3     0 H         2     0 H     J Moss     13
##  5 14/08/20… Everton  Southam…     3     1 H         0     1 A     A Madl…    14
##  6 14/08/20… Leicest… Wolves       1     0 H         1     0 H     C Paws…     9
##  7 14/08/20… Watford  Aston V…     3     2 H         2     0 H     M Dean     13
##  8 14/08/20… Norwich  Liverpo…     0     3 A         0     1 A     A Marr…    14
##  9 15/08/20… Newcast… West Ham     2     4 A         2     1 H     M Atki…    17
## 10 15/08/20… Tottenh… Man City     1     0 H         0     0 D     A Tayl…    13
## # ℹ 370 more rows
## # ℹ 11 more variables: AS <dbl>, HST <dbl>, AST <dbl>, HF <dbl>, AF <dbl>,
## #   HC <dbl>, AC <dbl>, HY <dbl>, AY <dbl>, HR <dbl>, AR <dbl>

Arrange rows

arrange(data, FTR)
## # A tibble: 380 × 22
##    Date      HomeTeam AwayTeam  FTHG  FTAG FTR    HTHG  HTAG HTR   Referee    HS
##    <chr>     <chr>    <chr>    <dbl> <dbl> <chr> <dbl> <dbl> <chr> <chr>   <dbl>
##  1 14/08/20… Burnley  Brighton     1     2 A         1     0 H     D Coote    14
##  2 14/08/20… Norwich  Liverpo…     0     3 A         0     1 A     A Marr…    14
##  3 15/08/20… Newcast… West Ham     2     4 A         2     1 H     M Atki…    17
##  4 22/08/20… Wolves   Tottenh…     0     1 A         0     1 A     S Attw…    25
##  5 22/08/20… Arsenal  Chelsea      0     2 A         0     2 A     P Tier…     6
##  6 28/08/20… Brighton Everton      0     2 A         0     1 A     J Moss     14
##  7 28/08/20… Norwich  Leicest…     1     2 A         1     1 D     R Jones    14
##  8 29/08/20… Wolves   Man Uni…     0     1 A         0     0 D     M Dean     15
##  9 44509     Brentfo… Brighton     0     1 A         0     0 D     G Scott     7
## 10 44509     Leicest… Man City     0     1 A         0     0 D     P Tier…     6
## # ℹ 370 more rows
## # ℹ 11 more variables: AS <dbl>, HST <dbl>, AST <dbl>, HF <dbl>, AF <dbl>,
## #   HC <dbl>, AC <dbl>, HY <dbl>, AY <dbl>, HR <dbl>, AR <dbl>
arrange(data, desc(FTHG))
## # A tibble: 380 × 22
##    Date      HomeTeam AwayTeam  FTHG  FTAG FTR    HTHG  HTAG HTR   Referee    HS
##    <chr>     <chr>    <chr>    <dbl> <dbl> <chr> <dbl> <dbl> <chr> <chr>   <dbl>
##  1 23/10/20… Chelsea  Norwich      7     0 H         3     0 H     A Madl…    23
##  2 14/12/20… Man City Leeds        7     0 H         3     0 H     P Tier…    31
##  3 26/12/20… Man City Leicest…     6     3 H         4     0 H     C Kava…    17
##  4 23/02/20… Liverpo… Leeds        6     0 H         3     0 H     M Oliv…    23
##  5 14/08/20… Man Uni… Leeds        5     1 H         1     0 H     P Tier…    16
##  6 21/08/20… Man City Norwich      5     0 H         2     0 H     G Scott    16
##  7 28/08/20… Man City Arsenal      5     0 H         3     0 H     M Atki…    25
##  8 44745     Tottenh… Everton      5     0 H         3     0 H     S Attw…    14
##  9 44624     Tottenh… Newcast…     5     1 H         1     1 D     M Atki…    19
## 10 23/04/20… Man City Watford      5     1 H         3     1 H     K Frie…    21
## # ℹ 370 more rows
## # ℹ 11 more variables: AS <dbl>, HST <dbl>, AST <dbl>, HF <dbl>, AF <dbl>,
## #   HC <dbl>, AC <dbl>, HY <dbl>, AY <dbl>, HR <dbl>, AR <dbl>
arrange(data, HomeTeam, desc(FTHG))
## # A tibble: 380 × 22
##    Date      HomeTeam AwayTeam  FTHG  FTAG FTR    HTHG  HTAG HTR   Referee    HS
##    <chr>     <chr>    <chr>    <dbl> <dbl> <chr> <dbl> <dbl> <chr> <chr>   <dbl>
##  1 22/05/20… Arsenal  Everton      5     1 H         2     1 H     A Marr…    26
##  2 26/09/20… Arsenal  Tottenh…     3     1 H         3     0 H     C Paws…    12
##  3 22/10/20… Arsenal  Aston V…     3     1 H         2     0 H     C Paws…    22
##  4 44512     Arsenal  Southam…     3     0 H         2     0 H     J Gill…    15
##  5 23/04/20… Arsenal  Man Uni…     3     1 H         2     1 H     C Paws…    14
##  6 18/10/20… Arsenal  Crystal…     2     2 D         1     0 H     M Dean     17
##  7 27/11/20… Arsenal  Newcast…     2     0 H         0     0 D     S Attw…    24
##  8 15/12/20… Arsenal  West Ham     2     0 H         0     0 D     A Tayl…    21
##  9 19/02/20… Arsenal  Brentfo…     2     1 H         0     0 D     J Moss     24
## 10 24/02/20… Arsenal  Wolves       2     1 H         0     1 A     M Atki…    26
## # ℹ 370 more rows
## # ℹ 11 more variables: AS <dbl>, HST <dbl>, AST <dbl>, HF <dbl>, AF <dbl>,
## #   HC <dbl>, AC <dbl>, HY <dbl>, AY <dbl>, HR <dbl>, AR <dbl>

Select columns

select(data, Date:AwayTeam)
## # A tibble: 380 × 3
##    Date       HomeTeam   AwayTeam      
##    <chr>      <chr>      <chr>         
##  1 13/08/2021 Brentford  Arsenal       
##  2 14/08/2021 Man United Leeds         
##  3 14/08/2021 Burnley    Brighton      
##  4 14/08/2021 Chelsea    Crystal Palace
##  5 14/08/2021 Everton    Southampton   
##  6 14/08/2021 Leicester  Wolves        
##  7 14/08/2021 Watford    Aston Villa   
##  8 14/08/2021 Norwich    Liverpool     
##  9 15/08/2021 Newcastle  West Ham      
## 10 15/08/2021 Tottenham  Man City      
## # ℹ 370 more rows
select(data, HomeTeam, AwayTeam, FTHG, FTAG)
## # A tibble: 380 × 4
##    HomeTeam   AwayTeam        FTHG  FTAG
##    <chr>      <chr>          <dbl> <dbl>
##  1 Brentford  Arsenal            2     0
##  2 Man United Leeds              5     1
##  3 Burnley    Brighton           1     2
##  4 Chelsea    Crystal Palace     3     0
##  5 Everton    Southampton        3     1
##  6 Leicester  Wolves             1     0
##  7 Watford    Aston Villa        3     2
##  8 Norwich    Liverpool          0     3
##  9 Newcastle  West Ham           2     4
## 10 Tottenham  Man City           1     0
## # ℹ 370 more rows
select(data, starts_with("HT"))
## # A tibble: 380 × 3
##     HTHG  HTAG HTR  
##    <dbl> <dbl> <chr>
##  1     1     0 H    
##  2     1     0 H    
##  3     1     0 H    
##  4     2     0 H    
##  5     0     1 A    
##  6     1     0 H    
##  7     2     0 H    
##  8     0     1 A    
##  9     2     1 H    
## 10     0     0 D    
## # ℹ 370 more rows
select(data, contains("Team"))
## # A tibble: 380 × 2
##    HomeTeam   AwayTeam      
##    <chr>      <chr>         
##  1 Brentford  Arsenal       
##  2 Man United Leeds         
##  3 Burnley    Brighton      
##  4 Chelsea    Crystal Palace
##  5 Everton    Southampton   
##  6 Leicester  Wolves        
##  7 Watford    Aston Villa   
##  8 Norwich    Liverpool     
##  9 Newcastle  West Ham      
## 10 Tottenham  Man City      
## # ℹ 370 more rows
select(data, ends_with("G"))
## # A tibble: 380 × 4
##     FTHG  FTAG  HTHG  HTAG
##    <dbl> <dbl> <dbl> <dbl>
##  1     2     0     1     0
##  2     5     1     1     0
##  3     1     2     1     0
##  4     3     0     2     0
##  5     3     1     0     1
##  6     1     0     1     0
##  7     3     2     2     0
##  8     0     3     0     1
##  9     2     4     2     1
## 10     1     0     0     0
## # ℹ 370 more rows
select(data, HomeTeam, AwayTeam, FTHG, FTAG, everything())
## # A tibble: 380 × 22
##    HomeTeam   AwayTeam    FTHG  FTAG Date  FTR    HTHG  HTAG HTR   Referee    HS
##    <chr>      <chr>      <dbl> <dbl> <chr> <chr> <dbl> <dbl> <chr> <chr>   <dbl>
##  1 Brentford  Arsenal        2     0 13/0… H         1     0 H     M Oliv…     8
##  2 Man United Leeds          5     1 14/0… H         1     0 H     P Tier…    16
##  3 Burnley    Brighton       1     2 14/0… A         1     0 H     D Coote    14
##  4 Chelsea    Crystal P…     3     0 14/0… H         2     0 H     J Moss     13
##  5 Everton    Southampt…     3     1 14/0… H         0     1 A     A Madl…    14
##  6 Leicester  Wolves         1     0 14/0… H         1     0 H     C Paws…     9
##  7 Watford    Aston Vil…     3     2 14/0… H         2     0 H     M Dean     13
##  8 Norwich    Liverpool      0     3 14/0… A         0     1 A     A Marr…    14
##  9 Newcastle  West Ham       2     4 15/0… A         2     1 H     M Atki…    17
## 10 Tottenham  Man City       1     0 15/0… H         0     0 D     A Tayl…    13
## # ℹ 370 more rows
## # ℹ 11 more variables: AS <dbl>, HST <dbl>, AST <dbl>, HF <dbl>, AF <dbl>,
## #   HC <dbl>, AC <dbl>, HY <dbl>, AY <dbl>, HR <dbl>, AR <dbl>

Add columns

# Total goals scored in the match
mutate(data, TotalGoals = FTHG + FTAG) %>%
    select(HomeTeam, AwayTeam, FTHG, FTAG, TotalGoals)
## # A tibble: 380 × 5
##    HomeTeam   AwayTeam        FTHG  FTAG TotalGoals
##    <chr>      <chr>          <dbl> <dbl>      <dbl>
##  1 Brentford  Arsenal            2     0          2
##  2 Man United Leeds              5     1          6
##  3 Burnley    Brighton           1     2          3
##  4 Chelsea    Crystal Palace     3     0          3
##  5 Everton    Southampton        3     1          4
##  6 Leicester  Wolves             1     0          1
##  7 Watford    Aston Villa        3     2          5
##  8 Norwich    Liverpool          0     3          3
##  9 Newcastle  West Ham           2     4          6
## 10 Tottenham  Man City           1     0          1
## # ℹ 370 more rows
# Just keep goal difference using transmute
transmute(data, GoalDiff = FTHG - FTAG)
## # A tibble: 380 × 1
##    GoalDiff
##       <dbl>
##  1        2
##  2        4
##  3       -1
##  4        3
##  5        2
##  6        1
##  7        1
##  8       -3
##  9       -2
## 10        1
## # ℹ 370 more rows
# Cumulative sum of home shots
select(data, HS) %>%
    mutate(HS_cumsum = cumsum(HS))
## # A tibble: 380 × 2
##       HS HS_cumsum
##    <dbl>     <dbl>
##  1     8         8
##  2    16        24
##  3    14        38
##  4    13        51
##  5    14        65
##  6     9        74
##  7    13        87
##  8    14       101
##  9    17       118
## 10    13       131
## # ℹ 370 more rows

Summarize by groups

# Overall average home goals
summarise(data, avg_home_goals = mean(FTHG, na.rm = TRUE))
## # A tibble: 1 × 1
##   avg_home_goals
##            <dbl>
## 1           1.51
# Group by Referee to see match counts and average home goals
data %>%
    group_by(Referee) %>%
    summarise(match_count = n(),
              avg_home_goals = mean(FTHG, na.rm = TRUE)) %>%
    arrange(desc(match_count))
## # A tibble: 22 × 3
##    Referee    match_count avg_home_goals
##    <chr>            <int>          <dbl>
##  1 A Taylor            28           1.57
##  2 P Tierney           27           1.63
##  3 C Pawson            26           1.35
##  4 M Atkinson          26           1.73
##  5 M Dean              26           1.35
##  6 M Oliver            26           1.77
##  7 J Moss              25           1.84
##  8 D Coote             20           0.95
##  9 S Attwell           20           2.05
## 10 A Marriner          19           1.21
## # ℹ 12 more rows
# Grouping multiple variables (e.g., counting matches by Home and Away teams)
data %>%
    group_by(HomeTeam, AwayTeam) %>%
    summarise(count = n()) %>%
    ungroup()
## `summarise()` has regrouped the output.
## ℹ Summaries were computed grouped by HomeTeam and AwayTeam.
## ℹ Output is grouped by HomeTeam.
## ℹ Use `summarise(.groups = "drop_last")` to silence this message.
## ℹ Use `summarise(.by = c(HomeTeam, AwayTeam))` for per-operation grouping
##   (`?dplyr::dplyr_by`) instead.
## # A tibble: 380 × 3
##    HomeTeam AwayTeam       count
##    <chr>    <chr>          <int>
##  1 Arsenal  Aston Villa        1
##  2 Arsenal  Brentford          1
##  3 Arsenal  Brighton           1
##  4 Arsenal  Burnley            1
##  5 Arsenal  Chelsea            1
##  6 Arsenal  Crystal Palace     1
##  7 Arsenal  Everton            1
##  8 Arsenal  Leeds              1
##  9 Arsenal  Leicester          1
## 10 Arsenal  Liverpool          1
## # ℹ 370 more rows