Load packages

library(tidyverse)
## -- Attaching packages ----------------------------------------------------------------------------------------- tidyverse 1.2.1 --
## v ggplot2 3.2.1     v purrr   0.3.2
## v tibble  2.1.3     v dplyr   0.8.3
## v tidyr   1.0.0     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.4.0
## -- Conflicts -------------------------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(rvest)
## Loading required package: xml2
## 
## Attaching package: 'rvest'
## The following object is masked from 'package:purrr':
## 
##     pluck
## The following object is masked from 'package:readr':
## 
##     guess_encoding
library(janitor)
## 
## Attaching package: 'janitor'
## The following objects are masked from 'package:stats':
## 
##     chisq.test, fisher.test
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
## 
##     date
library(pracma)
## 
## Attaching package: 'pracma'
## The following object is masked from 'package:purrr':
## 
##     cross

Gather data

Import PL 2018/2019 data from football statistics

library(readr)
results_18_19 <- read_csv("E0.csv")
## Parsed with column specification:
## cols(
##   .default = col_double(),
##   Div = col_character(),
##   Date = col_character(),
##   HomeTeam = col_character(),
##   AwayTeam = col_character(),
##   FTR = col_character(),
##   HTR = col_character(),
##   Referee = col_character()
## )
## See spec(...) for full column specifications.

Web scrape

Previous season’s results

PL

Scrape PL 2017/2018 to get how teams finished the league

url <- 'https://en.wikipedia.org/wiki/2017%E2%80%9318_Premier_League'

table_17_18 <- url %>% 
  read_html() %>% 
  html_node(xpath = '//*[@id="mw-content-text"]/div/table[5]') %>% 
  html_table()

One time write: write_csv(table_17_18, “table_17_18.csv”)

Remove special characters from PL table and bind it

tidyname <- gsub("\\s*\\([^\\)]+\\)","",as.character(table_17_18$`Team[ vte ]`))

tidyname <- enframe(tidyname) %>% 
  select(value) %>% 
  rename(team = value)

table_17_18 <- cbind(table_17_18, tidyname)

Championship

Scrape Championship 2017/2018 to get newly promoted teams

url <- 'https://en.wikipedia.org/wiki/2017%E2%80%9318_EFL_Championship'

table_17_18_championship <- url %>% 
  read_html() %>% 
  html_node(xpath = '//*[@id="mw-content-text"]/div/table[5]') %>% 
  html_table()

One time write: write_csv(table_17_18_championship, “table_17_18_championship.csv”)

Remove special characters from PL table and bind it

tidyname <- gsub("\\s*\\([^\\)]+\\)","",as.character(table_17_18_championship$`Team[ vte ]`))

tidyname <- enframe(tidyname) %>% 
  select(value) %>% 
  rename(team = value)

table_17_18_championship <- cbind(table_17_18_championship, tidyname)

Add a penalty for playing the championship

###add 20 to the position
table_17_18_championship$Pos <- table_17_18_championship$Pos + 20

### halve select columns
table_17_18_championship[,4:8] <- table_17_18_championship[,4:8] / 2
table_17_18_championship[,10] <- table_17_18_championship[,10] / 2

Drop useless columns prior to merge

table_17_18 <- table_17_18 %>% 
  select(-`Team[ vte ]`, -GD, -`Qualification or relegation`, -Pld )

table_17_18_championship <- table_17_18_championship %>% 
  select(- `Team[ vte ]`, -GD, -`Promotion, qualification or relegation`, -Pld )

Merge on top of one another

merged_table <- rbind(table_17_18, table_17_18_championship)

Get squad values/salaries, etc

Taken from: https://www.transfermarkt.co.uk/premier-league/startseite/wettbewerb/GB1/saison_id/2018/plus/1 Filtered for 2018/2019 year

url <- 'https://www.transfermarkt.co.uk/premier-league/startseite/wettbewerb/GB1/saison_id/2018/plus/1'

salaries <- url %>% 
  read_html() %>% 
  html_node(xpath = '//*[@id="yw1"]/table') %>% 
  html_table(fill = TRUE)

One time write: write_csv(salaries, “salaries.csv”)

Tidy very messy name and layout

salaries <- salaries %>% 
  remove_empty() %>% 
  clean_names() %>% 
  slice(2:21) %>% 
  select(-o_mv) %>% 
  select(1:9) %>% 
  select(-2, -4, -5) %>% 
  rename(team = club, squad_size = name_1, average_age = players_playing_abroad, foreigners = o_age_1, market_value = players_playing_abroad_1, average_market_value = total_market_value)
## value for "which" not specified, defaulting to c("rows", "cols")

Align names with merged table team names

salaries$team[salaries$team == "Liverpool FC"] <- "Liverpool"
salaries$team[salaries$team == "Arsenal FC"] <- "Arsenal"
salaries$team[salaries$team == "Everton FC"] <- "Everton"
salaries$team[salaries$team == "Burnley FC"] <- "Burnley"
salaries$team[salaries$team == "Southampton FC"] <- "Southampton"
salaries$team[salaries$team == "AFC Bournemouth"] <- "Bournemouth"
salaries$team[salaries$team == "Watford FC"] <- "Watford"
salaries$team[salaries$team == "Fulham FC"] <- "Fulham"
salaries$team[salaries$team == "Chelsea FC"] <- "Chelsea"

Join data

joined <- left_join(merged_table, salaries, by = "team" ) %>% 
  filter(squad_size != "NA")

Tidy joined data

Average age uses commas instead of decimal point

joined$average_age <- gsub("\\,", ".", joined$average_age)

joined$average_age <- as.numeric(joined$average_age)

There are pound signs and m for millions in columns

joined$market_value <- gsub("£", "", joined$market_value)
joined$market_value <- gsub("m", "", joined$market_value)

joined$market_value <- as.numeric(joined$market_value)



joined$average_market_value <- gsub("£", "", joined$average_market_value)
joined$average_market_value <- gsub("m", "", joined$average_market_value)

joined$average_market_value <- as.numeric(joined$average_market_value)

Look at results

Drop excess data from results

results_18_19 <- results_18_19 %>% 
  select(2:23)

Tidy + Calculate new columns

results_18_19 <-results_18_19 %>% 
  mutate(GD = FTHG - HTAG)  #goals scored by home team minus away team

  
results_18_19$Date <- as.Date(results_18_19$Date, "%d/%m/%Y") #convert from char to date

Create table of all results

Need to add games played list later

games_played <- c(1:38)

Get distinct teams

list <- results_18_19 %>% 
  select(HomeTeam) %>% 
  distinct() %>% 
  rename(team = HomeTeam)

list$HomeTeam <- paste0(list$team, "_Home")
list$AwayTeam <- paste0(list$team, "_Away")

list$HomeTeam <- str_replace(list$HomeTeam, " ", "_")
list$AwayTeam <- str_replace(list$AwayTeam, " ", "_")

Isolate home results

for (team in list$team){
  assign(team, filter(results_18_19, HomeTeam ==  team))
}

Man_United_Home <- `Man United`
Bournemouth_Home <- Bournemouth
Fulham_Home <- Fulham
Huddersfield_Home <- Huddersfield
Newcastle_Home <- Newcastle
Watford_Home <- Watford
Wolves_Home <-Wolves
Arsenal_Home <- Arsenal
Liverpool_Home <- Liverpool
Southampton_Home <- Southampton
Cardiff_Home <- Cardiff
Chelsea_Home <- Chelsea
Everton_Home <- Everton
Leicester_Home <- Leicester
Tottenham_Home <- Tottenham
West_Ham_Home <- `West Ham`
Brighton_Home <- Brighton
Burnley_Home <- Burnley
Man_City_Home <- `Man City`
Crystal_Palace_Home <- `Crystal Palace`

Isolate away results

for (team in list$team){
  assign(team, filter(results_18_19, AwayTeam ==  team))
}

Man_United_Away <- `Man United`
Bournemouth_Away <- Bournemouth
Fulham_Away <- Fulham
Huddersfield_Away <- Huddersfield
Newcastle_Away <- Newcastle
Watford_Away <- Watford
Wolves_Away <-Wolves
Arsenal_Away <- Arsenal
Liverpool_Away <- Liverpool
Southampton_Away <- Southampton
Cardiff_Away <- Cardiff
Chelsea_Away <- Chelsea
Everton_Away <- Everton
Leicester_Away <- Leicester
Tottenham_Away <- Tottenham
West_Ham_Away <- `West Ham`
Brighton_Away <- Brighton
Burnley_Away <- Burnley
Man_City_Away <- `Man City`
Crystal_Palace_Away <- `Crystal Palace`

Rename variables from home results

Arsenal_Home <- Arsenal_Home %>% rename(team = HomeTeam,
                           opposition_team = AwayTeam,
                           fulltime_goals = FTHG,
                           fulltime_goals_conceeded = FTAG,
                           halftime_goals = HTHG,
                           halftime_goals_conceeded = HTAG,
                           shots = HS,
                           opposition_shots = AS,
                           shots_on_target = HST,
                           opposition_shots_on_target = AST,
                           fouls = HF,
                           opposition_fouls = AF,
                           corners = HC,
                           opposition_corners = AC,
                           yellow_cards = HY,
                           opposition_yellow_cards = AY,
                           red_cards = HR,
                           opposition_red_cards = AR)

Bournemouth_Home <- Bournemouth_Home %>% rename(team = HomeTeam,
                           opposition_team = AwayTeam,
                           fulltime_goals = FTHG,
                           fulltime_goals_conceeded = FTAG,
                           halftime_goals = HTHG,
                           halftime_goals_conceeded = HTAG,
                           shots = HS,
                           opposition_shots = AS,
                           shots_on_target = HST,
                           opposition_shots_on_target = AST,
                           fouls = HF,
                           opposition_fouls = AF,
                           corners = HC,
                           opposition_corners = AC,
                           yellow_cards = HY,
                           opposition_yellow_cards = AY,
                           red_cards = HR,
                           opposition_red_cards = AR)


Brighton_Home <- Brighton_Home %>% rename(team = HomeTeam,
                           opposition_team = AwayTeam,
                           fulltime_goals = FTHG,
                           fulltime_goals_conceeded = FTAG,
                           halftime_goals = HTHG,
                           halftime_goals_conceeded = HTAG,
                           shots = HS,
                           opposition_shots = AS,
                           shots_on_target = HST,
                           opposition_shots_on_target = AST,
                           fouls = HF,
                           opposition_fouls = AF,
                           corners = HC,
                           opposition_corners = AC,
                           yellow_cards = HY,
                           opposition_yellow_cards = AY,
                           red_cards = HR,
                           opposition_red_cards = AR)




Burnley_Home <- Burnley_Home %>% rename(team = HomeTeam,
                           opposition_team = AwayTeam,
                           fulltime_goals = FTHG,
                           fulltime_goals_conceeded = FTAG,
                           halftime_goals = HTHG,
                           halftime_goals_conceeded = HTAG,
                           shots = HS,
                           opposition_shots = AS,
                           shots_on_target = HST,
                           opposition_shots_on_target = AST,
                           fouls = HF,
                           opposition_fouls = AF,
                           corners = HC,
                           opposition_corners = AC,
                           yellow_cards = HY,
                           opposition_yellow_cards = AY,
                           red_cards = HR,
                           opposition_red_cards = AR)



Cardiff_Home <- Cardiff_Home %>% rename(team = HomeTeam,
                           opposition_team = AwayTeam,
                           fulltime_goals = FTHG,
                           fulltime_goals_conceeded = FTAG,
                           halftime_goals = HTHG,
                           halftime_goals_conceeded = HTAG,
                           shots = HS,
                           opposition_shots = AS,
                           shots_on_target = HST,
                           opposition_shots_on_target = AST,
                           fouls = HF,
                           opposition_fouls = AF,
                           corners = HC,
                           opposition_corners = AC,
                           yellow_cards = HY,
                           opposition_yellow_cards = AY,
                           red_cards = HR,
                           opposition_red_cards = AR)



Chelsea_Home <- Chelsea_Home %>% rename(team = HomeTeam,
                           opposition_team = AwayTeam,
                           fulltime_goals = FTHG,
                           fulltime_goals_conceeded = FTAG,
                           halftime_goals = HTHG,
                           halftime_goals_conceeded = HTAG,
                           shots = HS,
                           opposition_shots = AS,
                           shots_on_target = HST,
                           opposition_shots_on_target = AST,
                           fouls = HF,
                           opposition_fouls = AF,
                           corners = HC,
                           opposition_corners = AC,
                           yellow_cards = HY,
                           opposition_yellow_cards = AY,
                           red_cards = HR,
                           opposition_red_cards = AR)





Crystal_Palace_Home <- Crystal_Palace_Home %>% rename(team = HomeTeam,
                           opposition_team = AwayTeam,
                           fulltime_goals = FTHG,
                           fulltime_goals_conceeded = FTAG,
                           halftime_goals = HTHG,
                           halftime_goals_conceeded = HTAG,
                           shots = HS,
                           opposition_shots = AS,
                           shots_on_target = HST,
                           opposition_shots_on_target = AST,
                           fouls = HF,
                           opposition_fouls = AF,
                           corners = HC,
                           opposition_corners = AC,
                           yellow_cards = HY,
                           opposition_yellow_cards = AY,
                           red_cards = HR,
                           opposition_red_cards = AR)



Everton_Home <- Everton_Home %>% rename(team = HomeTeam,
                           opposition_team = AwayTeam,
                           fulltime_goals = FTHG,
                           fulltime_goals_conceeded = FTAG,
                           halftime_goals = HTHG,
                           halftime_goals_conceeded = HTAG,
                           shots = HS,
                           opposition_shots = AS,
                           shots_on_target = HST,
                           opposition_shots_on_target = AST,
                           fouls = HF,
                           opposition_fouls = AF,
                           corners = HC,
                           opposition_corners = AC,
                           yellow_cards = HY,
                           opposition_yellow_cards = AY,
                           red_cards = HR,
                           opposition_red_cards = AR)


Fulham_Home <- Fulham_Home %>% rename(team = HomeTeam,
                           opposition_team = AwayTeam,
                           fulltime_goals = FTHG,
                           fulltime_goals_conceeded = FTAG,
                           halftime_goals = HTHG,
                           halftime_goals_conceeded = HTAG,
                           shots = HS,
                           opposition_shots = AS,
                           shots_on_target = HST,
                           opposition_shots_on_target = AST,
                           fouls = HF,
                           opposition_fouls = AF,
                           corners = HC,
                           opposition_corners = AC,
                           yellow_cards = HY,
                           opposition_yellow_cards = AY,
                           red_cards = HR,
                           opposition_red_cards = AR)



Huddersfield_Home <- Huddersfield_Home %>% rename(team = HomeTeam,
                           opposition_team = AwayTeam,
                           fulltime_goals = FTHG,
                           fulltime_goals_conceeded = FTAG,
                           halftime_goals = HTHG,
                           halftime_goals_conceeded = HTAG,
                           shots = HS,
                           opposition_shots = AS,
                           shots_on_target = HST,
                           opposition_shots_on_target = AST,
                           fouls = HF,
                           opposition_fouls = AF,
                           corners = HC,
                           opposition_corners = AC,
                           yellow_cards = HY,
                           opposition_yellow_cards = AY,
                           red_cards = HR,
                           opposition_red_cards = AR)


Leicester_Home <- Leicester_Home %>% rename(team = HomeTeam,
                           opposition_team = AwayTeam,
                           fulltime_goals = FTHG,
                           fulltime_goals_conceeded = FTAG,
                           halftime_goals = HTHG,
                           halftime_goals_conceeded = HTAG,
                           shots = HS,
                           opposition_shots = AS,
                           shots_on_target = HST,
                           opposition_shots_on_target = AST,
                           fouls = HF,
                           opposition_fouls = AF,
                           corners = HC,
                           opposition_corners = AC,
                           yellow_cards = HY,
                           opposition_yellow_cards = AY,
                           red_cards = HR,
                           opposition_red_cards = AR)


Liverpool_Home <- Liverpool_Home %>% rename(team = HomeTeam,
                           opposition_team = AwayTeam,
                           fulltime_goals = FTHG,
                           fulltime_goals_conceeded = FTAG,
                           halftime_goals = HTHG,
                           halftime_goals_conceeded = HTAG,
                           shots = HS,
                           opposition_shots = AS,
                           shots_on_target = HST,
                           opposition_shots_on_target = AST,
                           fouls = HF,
                           opposition_fouls = AF,
                           corners = HC,
                           opposition_corners = AC,
                           yellow_cards = HY,
                           opposition_yellow_cards = AY,
                           red_cards = HR,
                           opposition_red_cards = AR)


Man_City_Home <- Man_City_Home %>% rename(team = HomeTeam,
                           opposition_team = AwayTeam,
                           fulltime_goals = FTHG,
                           fulltime_goals_conceeded = FTAG,
                           halftime_goals = HTHG,
                           halftime_goals_conceeded = HTAG,
                           shots = HS,
                           opposition_shots = AS,
                           shots_on_target = HST,
                           opposition_shots_on_target = AST,
                           fouls = HF,
                           opposition_fouls = AF,
                           corners = HC,
                           opposition_corners = AC,
                           yellow_cards = HY,
                           opposition_yellow_cards = AY,
                           red_cards = HR,
                           opposition_red_cards = AR)


Man_United_Home <- Man_United_Home %>% rename(team = HomeTeam,
                           opposition_team = AwayTeam,
                           fulltime_goals = FTHG,
                           fulltime_goals_conceeded = FTAG,
                           halftime_goals = HTHG,
                           halftime_goals_conceeded = HTAG,
                           shots = HS,
                           opposition_shots = AS,
                           shots_on_target = HST,
                           opposition_shots_on_target = AST,
                           fouls = HF,
                           opposition_fouls = AF,
                           corners = HC,
                           opposition_corners = AC,
                           yellow_cards = HY,
                           opposition_yellow_cards = AY,
                           red_cards = HR,
                           opposition_red_cards = AR)



Newcastle_Home <- Newcastle_Home %>% rename(team = HomeTeam,
                           opposition_team = AwayTeam,
                           fulltime_goals = FTHG,
                           fulltime_goals_conceeded = FTAG,
                           halftime_goals = HTHG,
                           halftime_goals_conceeded = HTAG,
                           shots = HS,
                           opposition_shots = AS,
                           shots_on_target = HST,
                           opposition_shots_on_target = AST,
                           fouls = HF,
                           opposition_fouls = AF,
                           corners = HC,
                           opposition_corners = AC,
                           yellow_cards = HY,
                           opposition_yellow_cards = AY,
                           red_cards = HR,
                           opposition_red_cards = AR)


Southampton_Home <- Southampton_Home %>% rename(team = HomeTeam,
                           opposition_team = AwayTeam,
                           fulltime_goals = FTHG,
                           fulltime_goals_conceeded = FTAG,
                           halftime_goals = HTHG,
                           halftime_goals_conceeded = HTAG,
                           shots = HS,
                           opposition_shots = AS,
                           shots_on_target = HST,
                           opposition_shots_on_target = AST,
                           fouls = HF,
                           opposition_fouls = AF,
                           corners = HC,
                           opposition_corners = AC,
                           yellow_cards = HY,
                           opposition_yellow_cards = AY,
                           red_cards = HR,
                           opposition_red_cards = AR)


Tottenham_Home <- Tottenham_Home %>% rename(team = HomeTeam,
                           opposition_team = AwayTeam,
                           fulltime_goals = FTHG,
                           fulltime_goals_conceeded = FTAG,
                           halftime_goals = HTHG,
                           halftime_goals_conceeded = HTAG,
                           shots = HS,
                           opposition_shots = AS,
                           shots_on_target = HST,
                           opposition_shots_on_target = AST,
                           fouls = HF,
                           opposition_fouls = AF,
                           corners = HC,
                           opposition_corners = AC,
                           yellow_cards = HY,
                           opposition_yellow_cards = AY,
                           red_cards = HR,
                           opposition_red_cards = AR)


Watford_Home <- Watford_Home %>% rename(team = HomeTeam,
                           opposition_team = AwayTeam,
                           fulltime_goals = FTHG,
                           fulltime_goals_conceeded = FTAG,
                           halftime_goals = HTHG,
                           halftime_goals_conceeded = HTAG,
                           shots = HS,
                           opposition_shots = AS,
                           shots_on_target = HST,
                           opposition_shots_on_target = AST,
                           fouls = HF,
                           opposition_fouls = AF,
                           corners = HC,
                           opposition_corners = AC,
                           yellow_cards = HY,
                           opposition_yellow_cards = AY,
                           red_cards = HR,
                           opposition_red_cards = AR)


West_Ham_Home <- West_Ham_Home %>% rename(team = HomeTeam,
                           opposition_team = AwayTeam,
                           fulltime_goals = FTHG,
                           fulltime_goals_conceeded = FTAG,
                           halftime_goals = HTHG,
                           halftime_goals_conceeded = HTAG,
                           shots = HS,
                           opposition_shots = AS,
                           shots_on_target = HST,
                           opposition_shots_on_target = AST,
                           fouls = HF,
                           opposition_fouls = AF,
                           corners = HC,
                           opposition_corners = AC,
                           yellow_cards = HY,
                           opposition_yellow_cards = AY,
                           red_cards = HR,
                           opposition_red_cards = AR)


Wolves_Home <- Wolves_Home %>% rename(team = HomeTeam,
                           opposition_team = AwayTeam,
                           fulltime_goals = FTHG,
                           fulltime_goals_conceeded = FTAG,
                           halftime_goals = HTHG,
                           halftime_goals_conceeded = HTAG,
                           shots = HS,
                           opposition_shots = AS,
                           shots_on_target = HST,
                           opposition_shots_on_target = AST,
                           fouls = HF,
                           opposition_fouls = AF,
                           corners = HC,
                           opposition_corners = AC,
                           yellow_cards = HY,
                           opposition_yellow_cards = AY,
                           red_cards = HR,
                           opposition_red_cards = AR)

Do the same for Away team

Arsenal_Away <- Arsenal_Away %>% rename(team = AwayTeam,
                           opposition_team = HomeTeam,
                           fulltime_goals = FTAG,
                           fulltime_goals_conceeded = FTHG,
                           halftime_goals = HTAG,
                           halftime_goals_conceeded = HTHG,
                           shots = AS,
                           opposition_shots = HS,
                           shots_on_target = AST,
                           opposition_shots_on_target = HST,
                           fouls = AF,
                           opposition_fouls = HF,
                           corners = AC,
                           opposition_corners = HC,
                           yellow_cards = AY,
                           opposition_yellow_cards = HY,
                           red_cards = AR,
                           opposition_red_cards = HR)


Bournemouth_Away <- Bournemouth_Away %>% rename(team = AwayTeam,
                           opposition_team = HomeTeam,
                           fulltime_goals = FTAG,
                           fulltime_goals_conceeded = FTHG,
                           halftime_goals = HTAG,
                           halftime_goals_conceeded = HTHG,
                           shots = AS,
                           opposition_shots = HS,
                           shots_on_target = AST,
                           opposition_shots_on_target = HST,
                           fouls = AF,
                           opposition_fouls = HF,
                           corners = AC,
                           opposition_corners = HC,
                           yellow_cards = AY,
                           opposition_yellow_cards = HY,
                           red_cards = AR,
                           opposition_red_cards = HR)


Brighton_Away <- Brighton_Away %>% rename(team = AwayTeam,
                           opposition_team = HomeTeam,
                           fulltime_goals = FTAG,
                           fulltime_goals_conceeded = FTHG,
                           halftime_goals = HTAG,
                           halftime_goals_conceeded = HTHG,
                           shots = AS,
                           opposition_shots = HS,
                           shots_on_target = AST,
                           opposition_shots_on_target = HST,
                           fouls = AF,
                           opposition_fouls = HF,
                           corners = AC,
                           opposition_corners = HC,
                           yellow_cards = AY,
                           opposition_yellow_cards = HY,
                           red_cards = AR,
                           opposition_red_cards = HR)


Burnley_Away <- Burnley_Away %>% rename(team = AwayTeam,
                           opposition_team = HomeTeam,
                           fulltime_goals = FTAG,
                           fulltime_goals_conceeded = FTHG,
                           halftime_goals = HTAG,
                           halftime_goals_conceeded = HTHG,
                           shots = AS,
                           opposition_shots = HS,
                           shots_on_target = AST,
                           opposition_shots_on_target = HST,
                           fouls = AF,
                           opposition_fouls = HF,
                           corners = AC,
                           opposition_corners = HC,
                           yellow_cards = AY,
                           opposition_yellow_cards = HY,
                           red_cards = AR,
                           opposition_red_cards = HR)


Cardiff_Away <- Cardiff_Away %>% rename(team = AwayTeam,
                           opposition_team = HomeTeam,
                           fulltime_goals = FTAG,
                           fulltime_goals_conceeded = FTHG,
                           halftime_goals = HTAG,
                           halftime_goals_conceeded = HTHG,
                           shots = AS,
                           opposition_shots = HS,
                           shots_on_target = AST,
                           opposition_shots_on_target = HST,
                           fouls = AF,
                           opposition_fouls = HF,
                           corners = AC,
                           opposition_corners = HC,
                           yellow_cards = AY,
                           opposition_yellow_cards = HY,
                           red_cards = AR,
                           opposition_red_cards = HR)


Chelsea_Away <- Chelsea_Away %>% rename(team = AwayTeam,
                           opposition_team = HomeTeam,
                           fulltime_goals = FTAG,
                           fulltime_goals_conceeded = FTHG,
                           halftime_goals = HTAG,
                           halftime_goals_conceeded = HTHG,
                           shots = AS,
                           opposition_shots = HS,
                           shots_on_target = AST,
                           opposition_shots_on_target = HST,
                           fouls = AF,
                           opposition_fouls = HF,
                           corners = AC,
                           opposition_corners = HC,
                           yellow_cards = AY,
                           opposition_yellow_cards = HY,
                           red_cards = AR,
                           opposition_red_cards = HR)


Crystal_Palace_Away <- Crystal_Palace_Away %>% rename(team = AwayTeam,
                           opposition_team = HomeTeam,
                           fulltime_goals = FTAG,
                           fulltime_goals_conceeded = FTHG,
                           halftime_goals = HTAG,
                           halftime_goals_conceeded = HTHG,
                           shots = AS,
                           opposition_shots = HS,
                           shots_on_target = AST,
                           opposition_shots_on_target = HST,
                           fouls = AF,
                           opposition_fouls = HF,
                           corners = AC,
                           opposition_corners = HC,
                           yellow_cards = AY,
                           opposition_yellow_cards = HY,
                           red_cards = AR,
                           opposition_red_cards = HR)


Everton_Away <- Everton_Away %>% rename(team = AwayTeam,
                           opposition_team = HomeTeam,
                           fulltime_goals = FTAG,
                           fulltime_goals_conceeded = FTHG,
                           halftime_goals = HTAG,
                           halftime_goals_conceeded = HTHG,
                           shots = AS,
                           opposition_shots = HS,
                           shots_on_target = AST,
                           opposition_shots_on_target = HST,
                           fouls = AF,
                           opposition_fouls = HF,
                           corners = AC,
                           opposition_corners = HC,
                           yellow_cards = AY,
                           opposition_yellow_cards = HY,
                           red_cards = AR,
                           opposition_red_cards = HR)


Fulham_Away <- Fulham_Away %>% rename(team = AwayTeam,
                           opposition_team = HomeTeam,
                           fulltime_goals = FTAG,
                           fulltime_goals_conceeded = FTHG,
                           halftime_goals = HTAG,
                           halftime_goals_conceeded = HTHG,
                           shots = AS,
                           opposition_shots = HS,
                           shots_on_target = AST,
                           opposition_shots_on_target = HST,
                           fouls = AF,
                           opposition_fouls = HF,
                           corners = AC,
                           opposition_corners = HC,
                           yellow_cards = AY,
                           opposition_yellow_cards = HY,
                           red_cards = AR,
                           opposition_red_cards = HR) 


Huddersfield_Away <- Huddersfield_Away %>% rename(team = AwayTeam,
                           opposition_team = HomeTeam,
                           fulltime_goals = FTAG,
                           fulltime_goals_conceeded = FTHG,
                           halftime_goals = HTAG,
                           halftime_goals_conceeded = HTHG,
                           shots = AS,
                           opposition_shots = HS,
                           shots_on_target = AST,
                           opposition_shots_on_target = HST,
                           fouls = AF,
                           opposition_fouls = HF,
                           corners = AC,
                           opposition_corners = HC,
                           yellow_cards = AY,
                           opposition_yellow_cards = HY,
                           red_cards = AR,
                           opposition_red_cards = HR) 


Leicester_Away <- Leicester_Away %>% rename(team = AwayTeam,
                           opposition_team = HomeTeam,
                           fulltime_goals = FTAG,
                           fulltime_goals_conceeded = FTHG,
                           halftime_goals = HTAG,
                           halftime_goals_conceeded = HTHG,
                           shots = AS,
                           opposition_shots = HS,
                           shots_on_target = AST,
                           opposition_shots_on_target = HST,
                           fouls = AF,
                           opposition_fouls = HF,
                           corners = AC,
                           opposition_corners = HC,
                           yellow_cards = AY,
                           opposition_yellow_cards = HY,
                           red_cards = AR,
                           opposition_red_cards = HR) 


Liverpool_Away <- Liverpool_Away %>% rename(team = AwayTeam,
                           opposition_team = HomeTeam,
                           fulltime_goals = FTAG,
                           fulltime_goals_conceeded = FTHG,
                           halftime_goals = HTAG,
                           halftime_goals_conceeded = HTHG,
                           shots = AS,
                           opposition_shots = HS,
                           shots_on_target = AST,
                           opposition_shots_on_target = HST,
                           fouls = AF,
                           opposition_fouls = HF,
                           corners = AC,
                           opposition_corners = HC,
                           yellow_cards = AY,
                           opposition_yellow_cards = HY,
                           red_cards = AR,
                           opposition_red_cards = HR) 


Man_City_Away <- Man_City_Away %>% rename(team = AwayTeam,
                           opposition_team = HomeTeam,
                           fulltime_goals = FTAG,
                           fulltime_goals_conceeded = FTHG,
                           halftime_goals = HTAG,
                           halftime_goals_conceeded = HTHG,
                           shots = AS,
                           opposition_shots = HS,
                           shots_on_target = AST,
                           opposition_shots_on_target = HST,
                           fouls = AF,
                           opposition_fouls = HF,
                           corners = AC,
                           opposition_corners = HC,
                           yellow_cards = AY,
                           opposition_yellow_cards = HY,
                           red_cards = AR,
                           opposition_red_cards = HR) 


Man_United_Away <- Man_United_Away %>% rename(team = AwayTeam,
                           opposition_team = HomeTeam,
                           fulltime_goals = FTAG,
                           fulltime_goals_conceeded = FTHG,
                           halftime_goals = HTAG,
                           halftime_goals_conceeded = HTHG,
                           shots = AS,
                           opposition_shots = HS,
                           shots_on_target = AST,
                           opposition_shots_on_target = HST,
                           fouls = AF,
                           opposition_fouls = HF,
                           corners = AC,
                           opposition_corners = HC,
                           yellow_cards = AY,
                           opposition_yellow_cards = HY,
                           red_cards = AR,
                           opposition_red_cards = HR)


Newcastle_Away <- Newcastle_Away %>% rename(team = AwayTeam,
                           opposition_team = HomeTeam,
                           fulltime_goals = FTAG,
                           fulltime_goals_conceeded = FTHG,
                           halftime_goals = HTAG,
                           halftime_goals_conceeded = HTHG,
                           shots = AS,
                           opposition_shots = HS,
                           shots_on_target = AST,
                           opposition_shots_on_target = HST,
                           fouls = AF,
                           opposition_fouls = HF,
                           corners = AC,
                           opposition_corners = HC,
                           yellow_cards = AY,
                           opposition_yellow_cards = HY,
                           red_cards = AR,
                           opposition_red_cards = HR)


Southampton_Away <- Southampton_Away %>% rename(team = AwayTeam,
                           opposition_team = HomeTeam,
                           fulltime_goals = FTAG,
                           fulltime_goals_conceeded = FTHG,
                           halftime_goals = HTAG,
                           halftime_goals_conceeded = HTHG,
                           shots = AS,
                           opposition_shots = HS,
                           shots_on_target = AST,
                           opposition_shots_on_target = HST,
                           fouls = AF,
                           opposition_fouls = HF,
                           corners = AC,
                           opposition_corners = HC,
                           yellow_cards = AY,
                           opposition_yellow_cards = HY,
                           red_cards = AR,
                           opposition_red_cards = HR)


Tottenham_Away <- Tottenham_Away %>% rename(team = AwayTeam,
                           opposition_team = HomeTeam,
                           fulltime_goals = FTAG,
                           fulltime_goals_conceeded = FTHG,
                           halftime_goals = HTAG,
                           halftime_goals_conceeded = HTHG,
                           shots = AS,
                           opposition_shots = HS,
                           shots_on_target = AST,
                           opposition_shots_on_target = HST,
                           fouls = AF,
                           opposition_fouls = HF,
                           corners = AC,
                           opposition_corners = HC,
                           yellow_cards = AY,
                           opposition_yellow_cards = HY,
                           red_cards = AR,
                           opposition_red_cards = HR)


Watford_Away <- Watford_Away %>% rename(team = AwayTeam,
                           opposition_team = HomeTeam,
                           fulltime_goals = FTAG,
                           fulltime_goals_conceeded = FTHG,
                           halftime_goals = HTAG,
                           halftime_goals_conceeded = HTHG,
                           shots = AS,
                           opposition_shots = HS,
                           shots_on_target = AST,
                           opposition_shots_on_target = HST,
                           fouls = AF,
                           opposition_fouls = HF,
                           corners = AC,
                           opposition_corners = HC,
                           yellow_cards = AY,
                           opposition_yellow_cards = HY,
                           red_cards = AR,
                           opposition_red_cards = HR)


West_Ham_Away <- West_Ham_Away %>% rename(team = AwayTeam,
                           opposition_team = HomeTeam,
                           fulltime_goals = FTAG,
                           fulltime_goals_conceeded = FTHG,
                           halftime_goals = HTAG,
                           halftime_goals_conceeded = HTHG,
                           shots = AS,
                           opposition_shots = HS,
                           shots_on_target = AST,
                           opposition_shots_on_target = HST,
                           fouls = AF,
                           opposition_fouls = HF,
                           corners = AC,
                           opposition_corners = HC,
                           yellow_cards = AY,
                           opposition_yellow_cards = HY,
                           red_cards = AR,
                           opposition_red_cards = HR)


Wolves_Away <- Wolves_Away %>% rename(team = AwayTeam,
                           opposition_team = HomeTeam,
                           fulltime_goals = FTAG,
                           fulltime_goals_conceeded = FTHG,
                           halftime_goals = HTAG,
                           halftime_goals_conceeded = HTHG,
                           shots = AS,
                           opposition_shots = HS,
                           shots_on_target = AST,
                           opposition_shots_on_target = HST,
                           fouls = AF,
                           opposition_fouls = HF,
                           corners = AC,
                           opposition_corners = HC,
                           yellow_cards = AY,
                           opposition_yellow_cards = HY,
                           red_cards = AR,
                           opposition_red_cards = HR)

Bind it all togther

Arsenal

Arsenal <- rbind(Arsenal_Home, Arsenal_Away) %>% 
  arrange(Date) %>% 
  mutate(points_gained = ifelse(fulltime_goals > fulltime_goals_conceeded, 3, ifelse(fulltime_goals == fulltime_goals_conceeded, sample(1, length(fulltime_goals), replace = TRUE), 0))) %>% 
  cbind(games_played) %>% 
                                                                                          ###calculate totals
  mutate(total_points_gained = cumsum(points_gained),
         total_fulltime_goals_scored = cumsum(fulltime_goals),
         total_fulltime_goals_conceeded = cumsum(fulltime_goals_conceeded),
         total_halftime_goals = cumsum(halftime_goals),
         total_halftime_goals_conceeded = cumsum(halftime_goals_conceeded),
         total_shots = cumsum(shots),
         total_opposition_shots = cumsum(opposition_shots),
         total_shots_on_target = cumsum(shots_on_target),
         total_opposition_shots_on_target = cumsum(opposition_shots_on_target),
         total_fouls = cumsum(fouls),
         total_opposition_fouls = cumsum(opposition_fouls),
         total_corners = cumsum(corners),
         total_opposition_corners = cumsum(opposition_corners),
         total_yellow_cards = cumsum(yellow_cards),
         total_opposition_yellow_cards = cumsum(opposition_yellow_cards),
         total_red_cards = cumsum(red_cards),
         total_opposition_red_cards = cumsum(opposition_red_cards)) %>% 
                                                                                          ###calcualte averages
   mutate(average_points_gained = total_points_gained/games_played,
         average_fulltime_goals_scored = total_fulltime_goals_scored/games_played,
         average_fulltime_goals_conceeded = total_fulltime_goals_conceeded/games_played,
         average_halftime_goals = total_halftime_goals/games_played,
         average_halftime_goals_conceeded = total_halftime_goals_conceeded/games_played,
         average_shots = total_shots/games_played,
         average_opposition_shots = total_opposition_shots/games_played,
         average_shots_on_target = total_shots_on_target/games_played,
         average_opposition_shots_on_target = total_opposition_shots_on_target/games_played,
         average_fouls = total_fouls/games_played,
         average_opposition_fouls = total_opposition_fouls/games_played,
         average_corners = total_corners/games_played,
         average_opposition_corners = total_opposition_corners/games_played,
         average_yellow_cards = total_yellow_cards/games_played,
         average_opposition_yellow_cards = total_opposition_yellow_cards/games_played,
         average_red_cards = total_red_cards/games_played,
         average_opposition_red_cards = total_opposition_red_cards/games_played) %>% 
                                                                                         ###calculate form
  mutate(points_gained_form = movavg(points_gained, 6),
         fulltime_goals_form = movavg(fulltime_goals, 6),
         fulltime_goals_conceeded_form = movavg(fulltime_goals_conceeded, 6),
         halftime_goals_form = movavg(halftime_goals, 6),
         halftime_goals_conceeded_form = movavg(halftime_goals_conceeded, 6),
         shots_form = movavg(shots, 6),
         opposition_shots_form = movavg(opposition_shots, 6),
         shots_on_target_form = movavg(shots_on_target, 6),
         opposition_shots_on_target_form = movavg(opposition_shots_on_target, 6),
         fouls_form = movavg(fouls, 6),
         opposition_fouls_form = movavg(opposition_fouls, 6),
         corners_form = movavg(corners, 6),
         opposition_corners_form = movavg(opposition_corners, 6),
         yellow_cards_form = movavg(yellow_cards, 6),
         opposition_yellow_cards_form = movavg(opposition_yellow_cards, 6),
         red_cards_form = movavg(red_cards, 6),
         opposition_red_cards_form = movavg(opposition_red_cards, 6))
## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

Bournemouth

Bournemouth <- rbind(Bournemouth_Home, Bournemouth_Away) %>% 
  arrange(Date) %>% 
  mutate(points_gained = ifelse(fulltime_goals > fulltime_goals_conceeded, 3, ifelse(fulltime_goals == fulltime_goals_conceeded, sample(1, length(fulltime_goals), replace = TRUE), 0))) %>% 
  cbind(games_played) %>% 
                                                                                          ###calculate totals
  mutate(total_points_gained = cumsum(points_gained),
         total_fulltime_goals_scored = cumsum(fulltime_goals),
         total_fulltime_goals_conceeded = cumsum(fulltime_goals_conceeded),
         total_halftime_goals = cumsum(halftime_goals),
         total_halftime_goals_conceeded = cumsum(halftime_goals_conceeded),
         total_shots = cumsum(shots),
         total_opposition_shots = cumsum(opposition_shots),
         total_shots_on_target = cumsum(shots_on_target),
         total_opposition_shots_on_target = cumsum(opposition_shots_on_target),
         total_fouls = cumsum(fouls),
         total_opposition_fouls = cumsum(opposition_fouls),
         total_corners = cumsum(corners),
         total_opposition_corners = cumsum(opposition_corners),
         total_yellow_cards = cumsum(yellow_cards),
         total_opposition_yellow_cards = cumsum(opposition_yellow_cards),
         total_red_cards = cumsum(red_cards),
         total_opposition_red_cards = cumsum(opposition_red_cards)) %>% 
                                                                                          ###calcualte averages
   mutate(average_points_gained = total_points_gained/games_played,
         average_fulltime_goals_scored = total_fulltime_goals_scored/games_played,
         average_fulltime_goals_conceeded = total_fulltime_goals_conceeded/games_played,
         average_halftime_goals = total_halftime_goals/games_played,
         average_halftime_goals_conceeded = total_halftime_goals_conceeded/games_played,
         average_shots = total_shots/games_played,
         average_opposition_shots = total_opposition_shots/games_played,
         average_shots_on_target = total_shots_on_target/games_played,
         average_opposition_shots_on_target = total_opposition_shots_on_target/games_played,
         average_fouls = total_fouls/games_played,
         average_opposition_fouls = total_opposition_fouls/games_played,
         average_corners = total_corners/games_played,
         average_opposition_corners = total_opposition_corners/games_played,
         average_yellow_cards = total_yellow_cards/games_played,
         average_opposition_yellow_cards = total_opposition_yellow_cards/games_played,
         average_red_cards = total_red_cards/games_played,
         average_opposition_red_cards = total_opposition_red_cards/games_played) %>% 
                                                                                         ###calculate form
  mutate(points_gained_form = movavg(points_gained, 6),
         fulltime_goals_form = movavg(fulltime_goals, 6),
         fulltime_goals_conceeded_form = movavg(fulltime_goals_conceeded, 6),
         halftime_goals_form = movavg(halftime_goals, 6),
         halftime_goals_conceeded_form = movavg(halftime_goals_conceeded, 6),
         shots_form = movavg(shots, 6),
         opposition_shots_form = movavg(opposition_shots, 6),
         shots_on_target_form = movavg(shots_on_target, 6),
         opposition_shots_on_target_form = movavg(opposition_shots_on_target, 6),
         fouls_form = movavg(fouls, 6),
         opposition_fouls_form = movavg(opposition_fouls, 6),
         corners_form = movavg(corners, 6),
         opposition_corners_form = movavg(opposition_corners, 6),
         yellow_cards_form = movavg(yellow_cards, 6),
         opposition_yellow_cards_form = movavg(opposition_yellow_cards, 6),
         red_cards_form = movavg(red_cards, 6),
         opposition_red_cards_form = movavg(opposition_red_cards, 6))
## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

Brighton

Brighton <- rbind(Brighton_Home, Brighton_Away) %>% 
  arrange(Date) %>% 
  mutate(points_gained = ifelse(fulltime_goals > fulltime_goals_conceeded, 3, ifelse(fulltime_goals == fulltime_goals_conceeded, sample(1, length(fulltime_goals), replace = TRUE), 0))) %>% 
  cbind(games_played) %>% 
                                                                                          ###calculate totals
  mutate(total_points_gained = cumsum(points_gained),
         total_fulltime_goals_scored = cumsum(fulltime_goals),
         total_fulltime_goals_conceeded = cumsum(fulltime_goals_conceeded),
         total_halftime_goals = cumsum(halftime_goals),
         total_halftime_goals_conceeded = cumsum(halftime_goals_conceeded),
         total_shots = cumsum(shots),
         total_opposition_shots = cumsum(opposition_shots),
         total_shots_on_target = cumsum(shots_on_target),
         total_opposition_shots_on_target = cumsum(opposition_shots_on_target),
         total_fouls = cumsum(fouls),
         total_opposition_fouls = cumsum(opposition_fouls),
         total_corners = cumsum(corners),
         total_opposition_corners = cumsum(opposition_corners),
         total_yellow_cards = cumsum(yellow_cards),
         total_opposition_yellow_cards = cumsum(opposition_yellow_cards),
         total_red_cards = cumsum(red_cards),
         total_opposition_red_cards = cumsum(opposition_red_cards)) %>% 
                                                                                          ###calcualte averages
   mutate(average_points_gained = total_points_gained/games_played,
         average_fulltime_goals_scored = total_fulltime_goals_scored/games_played,
         average_fulltime_goals_conceeded = total_fulltime_goals_conceeded/games_played,
         average_halftime_goals = total_halftime_goals/games_played,
         average_halftime_goals_conceeded = total_halftime_goals_conceeded/games_played,
         average_shots = total_shots/games_played,
         average_opposition_shots = total_opposition_shots/games_played,
         average_shots_on_target = total_shots_on_target/games_played,
         average_opposition_shots_on_target = total_opposition_shots_on_target/games_played,
         average_fouls = total_fouls/games_played,
         average_opposition_fouls = total_opposition_fouls/games_played,
         average_corners = total_corners/games_played,
         average_opposition_corners = total_opposition_corners/games_played,
         average_yellow_cards = total_yellow_cards/games_played,
         average_opposition_yellow_cards = total_opposition_yellow_cards/games_played,
         average_red_cards = total_red_cards/games_played,
         average_opposition_red_cards = total_opposition_red_cards/games_played) %>% 
                                                                                         ###calculate form
  mutate(points_gained_form = movavg(points_gained, 6),
         fulltime_goals_form = movavg(fulltime_goals, 6),
         fulltime_goals_conceeded_form = movavg(fulltime_goals_conceeded, 6),
         halftime_goals_form = movavg(halftime_goals, 6),
         halftime_goals_conceeded_form = movavg(halftime_goals_conceeded, 6),
         shots_form = movavg(shots, 6),
         opposition_shots_form = movavg(opposition_shots, 6),
         shots_on_target_form = movavg(shots_on_target, 6),
         opposition_shots_on_target_form = movavg(opposition_shots_on_target, 6),
         fouls_form = movavg(fouls, 6),
         opposition_fouls_form = movavg(opposition_fouls, 6),
         corners_form = movavg(corners, 6),
         opposition_corners_form = movavg(opposition_corners, 6),
         yellow_cards_form = movavg(yellow_cards, 6),
         opposition_yellow_cards_form = movavg(opposition_yellow_cards, 6),
         red_cards_form = movavg(red_cards, 6),
         opposition_red_cards_form = movavg(opposition_red_cards, 6))
## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

Burnley

Burnley <- rbind(Burnley_Home, Burnley_Away) %>% 
  arrange(Date) %>% 
  mutate(points_gained = ifelse(fulltime_goals > fulltime_goals_conceeded, 3, ifelse(fulltime_goals == fulltime_goals_conceeded, sample(1, length(fulltime_goals), replace = TRUE), 0))) %>% 
  cbind(games_played) %>% 
                                                                                          ###calculate totals
  mutate(total_points_gained = cumsum(points_gained),
         total_fulltime_goals_scored = cumsum(fulltime_goals),
         total_fulltime_goals_conceeded = cumsum(fulltime_goals_conceeded),
         total_halftime_goals = cumsum(halftime_goals),
         total_halftime_goals_conceeded = cumsum(halftime_goals_conceeded),
         total_shots = cumsum(shots),
         total_opposition_shots = cumsum(opposition_shots),
         total_shots_on_target = cumsum(shots_on_target),
         total_opposition_shots_on_target = cumsum(opposition_shots_on_target),
         total_fouls = cumsum(fouls),
         total_opposition_fouls = cumsum(opposition_fouls),
         total_corners = cumsum(corners),
         total_opposition_corners = cumsum(opposition_corners),
         total_yellow_cards = cumsum(yellow_cards),
         total_opposition_yellow_cards = cumsum(opposition_yellow_cards),
         total_red_cards = cumsum(red_cards),
         total_opposition_red_cards = cumsum(opposition_red_cards)) %>% 
                                                                                          ###calcualte averages
   mutate(average_points_gained = total_points_gained/games_played,
         average_fulltime_goals_scored = total_fulltime_goals_scored/games_played,
         average_fulltime_goals_conceeded = total_fulltime_goals_conceeded/games_played,
         average_halftime_goals = total_halftime_goals/games_played,
         average_halftime_goals_conceeded = total_halftime_goals_conceeded/games_played,
         average_shots = total_shots/games_played,
         average_opposition_shots = total_opposition_shots/games_played,
         average_shots_on_target = total_shots_on_target/games_played,
         average_opposition_shots_on_target = total_opposition_shots_on_target/games_played,
         average_fouls = total_fouls/games_played,
         average_opposition_fouls = total_opposition_fouls/games_played,
         average_corners = total_corners/games_played,
         average_opposition_corners = total_opposition_corners/games_played,
         average_yellow_cards = total_yellow_cards/games_played,
         average_opposition_yellow_cards = total_opposition_yellow_cards/games_played,
         average_red_cards = total_red_cards/games_played,
         average_opposition_red_cards = total_opposition_red_cards/games_played) %>% 
                                                                                         ###calculate form
  mutate(points_gained_form = movavg(points_gained, 6),
         fulltime_goals_form = movavg(fulltime_goals, 6),
         fulltime_goals_conceeded_form = movavg(fulltime_goals_conceeded, 6),
         halftime_goals_form = movavg(halftime_goals, 6),
         halftime_goals_conceeded_form = movavg(halftime_goals_conceeded, 6),
         shots_form = movavg(shots, 6),
         opposition_shots_form = movavg(opposition_shots, 6),
         shots_on_target_form = movavg(shots_on_target, 6),
         opposition_shots_on_target_form = movavg(opposition_shots_on_target, 6),
         fouls_form = movavg(fouls, 6),
         opposition_fouls_form = movavg(opposition_fouls, 6),
         corners_form = movavg(corners, 6),
         opposition_corners_form = movavg(opposition_corners, 6),
         yellow_cards_form = movavg(yellow_cards, 6),
         opposition_yellow_cards_form = movavg(opposition_yellow_cards, 6),
         red_cards_form = movavg(red_cards, 6),
         opposition_red_cards_form = movavg(opposition_red_cards, 6))
## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

Cardiff

Cardiff <- rbind(Cardiff_Home, Cardiff_Away) %>% 
  arrange(Date) %>% 
  mutate(points_gained = ifelse(fulltime_goals > fulltime_goals_conceeded, 3, ifelse(fulltime_goals == fulltime_goals_conceeded, sample(1, length(fulltime_goals), replace = TRUE), 0))) %>% 
  cbind(games_played) %>% 
                                                                                          ###calculate totals
  mutate(total_points_gained = cumsum(points_gained),
         total_fulltime_goals_scored = cumsum(fulltime_goals),
         total_fulltime_goals_conceeded = cumsum(fulltime_goals_conceeded),
         total_halftime_goals = cumsum(halftime_goals),
         total_halftime_goals_conceeded = cumsum(halftime_goals_conceeded),
         total_shots = cumsum(shots),
         total_opposition_shots = cumsum(opposition_shots),
         total_shots_on_target = cumsum(shots_on_target),
         total_opposition_shots_on_target = cumsum(opposition_shots_on_target),
         total_fouls = cumsum(fouls),
         total_opposition_fouls = cumsum(opposition_fouls),
         total_corners = cumsum(corners),
         total_opposition_corners = cumsum(opposition_corners),
         total_yellow_cards = cumsum(yellow_cards),
         total_opposition_yellow_cards = cumsum(opposition_yellow_cards),
         total_red_cards = cumsum(red_cards),
         total_opposition_red_cards = cumsum(opposition_red_cards)) %>% 
                                                                                          ###calcualte averages
   mutate(average_points_gained = total_points_gained/games_played,
         average_fulltime_goals_scored = total_fulltime_goals_scored/games_played,
         average_fulltime_goals_conceeded = total_fulltime_goals_conceeded/games_played,
         average_halftime_goals = total_halftime_goals/games_played,
         average_halftime_goals_conceeded = total_halftime_goals_conceeded/games_played,
         average_shots = total_shots/games_played,
         average_opposition_shots = total_opposition_shots/games_played,
         average_shots_on_target = total_shots_on_target/games_played,
         average_opposition_shots_on_target = total_opposition_shots_on_target/games_played,
         average_fouls = total_fouls/games_played,
         average_opposition_fouls = total_opposition_fouls/games_played,
         average_corners = total_corners/games_played,
         average_opposition_corners = total_opposition_corners/games_played,
         average_yellow_cards = total_yellow_cards/games_played,
         average_opposition_yellow_cards = total_opposition_yellow_cards/games_played,
         average_red_cards = total_red_cards/games_played,
         average_opposition_red_cards = total_opposition_red_cards/games_played) %>% 
                                                                                         ###calculate form
  mutate(points_gained_form = movavg(points_gained, 6),
         fulltime_goals_form = movavg(fulltime_goals, 6),
         fulltime_goals_conceeded_form = movavg(fulltime_goals_conceeded, 6),
         halftime_goals_form = movavg(halftime_goals, 6),
         halftime_goals_conceeded_form = movavg(halftime_goals_conceeded, 6),
         shots_form = movavg(shots, 6),
         opposition_shots_form = movavg(opposition_shots, 6),
         shots_on_target_form = movavg(shots_on_target, 6),
         opposition_shots_on_target_form = movavg(opposition_shots_on_target, 6),
         fouls_form = movavg(fouls, 6),
         opposition_fouls_form = movavg(opposition_fouls, 6),
         corners_form = movavg(corners, 6),
         opposition_corners_form = movavg(opposition_corners, 6),
         yellow_cards_form = movavg(yellow_cards, 6),
         opposition_yellow_cards_form = movavg(opposition_yellow_cards, 6),
         red_cards_form = movavg(red_cards, 6),
         opposition_red_cards_form = movavg(opposition_red_cards, 6))
## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

Chelsea

Chelsea <- rbind(Chelsea_Home, Chelsea_Away) %>% 
  arrange(Date) %>% 
  mutate(points_gained = ifelse(fulltime_goals > fulltime_goals_conceeded, 3, ifelse(fulltime_goals == fulltime_goals_conceeded, sample(1, length(fulltime_goals), replace = TRUE), 0))) %>% 
  cbind(games_played) %>% 
                                                                                          ###calculate totals
  mutate(total_points_gained = cumsum(points_gained),
         total_fulltime_goals_scored = cumsum(fulltime_goals),
         total_fulltime_goals_conceeded = cumsum(fulltime_goals_conceeded),
         total_halftime_goals = cumsum(halftime_goals),
         total_halftime_goals_conceeded = cumsum(halftime_goals_conceeded),
         total_shots = cumsum(shots),
         total_opposition_shots = cumsum(opposition_shots),
         total_shots_on_target = cumsum(shots_on_target),
         total_opposition_shots_on_target = cumsum(opposition_shots_on_target),
         total_fouls = cumsum(fouls),
         total_opposition_fouls = cumsum(opposition_fouls),
         total_corners = cumsum(corners),
         total_opposition_corners = cumsum(opposition_corners),
         total_yellow_cards = cumsum(yellow_cards),
         total_opposition_yellow_cards = cumsum(opposition_yellow_cards),
         total_red_cards = cumsum(red_cards),
         total_opposition_red_cards = cumsum(opposition_red_cards)) %>% 
                                                                                          ###calcualte averages
   mutate(average_points_gained = total_points_gained/games_played,
         average_fulltime_goals_scored = total_fulltime_goals_scored/games_played,
         average_fulltime_goals_conceeded = total_fulltime_goals_conceeded/games_played,
         average_halftime_goals = total_halftime_goals/games_played,
         average_halftime_goals_conceeded = total_halftime_goals_conceeded/games_played,
         average_shots = total_shots/games_played,
         average_opposition_shots = total_opposition_shots/games_played,
         average_shots_on_target = total_shots_on_target/games_played,
         average_opposition_shots_on_target = total_opposition_shots_on_target/games_played,
         average_fouls = total_fouls/games_played,
         average_opposition_fouls = total_opposition_fouls/games_played,
         average_corners = total_corners/games_played,
         average_opposition_corners = total_opposition_corners/games_played,
         average_yellow_cards = total_yellow_cards/games_played,
         average_opposition_yellow_cards = total_opposition_yellow_cards/games_played,
         average_red_cards = total_red_cards/games_played,
         average_opposition_red_cards = total_opposition_red_cards/games_played) %>% 
                                                                                         ###calculate form
  mutate(points_gained_form = movavg(points_gained, 6),
         fulltime_goals_form = movavg(fulltime_goals, 6),
         fulltime_goals_conceeded_form = movavg(fulltime_goals_conceeded, 6),
         halftime_goals_form = movavg(halftime_goals, 6),
         halftime_goals_conceeded_form = movavg(halftime_goals_conceeded, 6),
         shots_form = movavg(shots, 6),
         opposition_shots_form = movavg(opposition_shots, 6),
         shots_on_target_form = movavg(shots_on_target, 6),
         opposition_shots_on_target_form = movavg(opposition_shots_on_target, 6),
         fouls_form = movavg(fouls, 6),
         opposition_fouls_form = movavg(opposition_fouls, 6),
         corners_form = movavg(corners, 6),
         opposition_corners_form = movavg(opposition_corners, 6),
         yellow_cards_form = movavg(yellow_cards, 6),
         opposition_yellow_cards_form = movavg(opposition_yellow_cards, 6),
         red_cards_form = movavg(red_cards, 6),
         opposition_red_cards_form = movavg(opposition_red_cards, 6))
## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

Crystal Palace

Crystal_Palace <- rbind(Crystal_Palace_Home, Crystal_Palace_Away) %>% 
  arrange(Date) %>% 
  mutate(points_gained = ifelse(fulltime_goals > fulltime_goals_conceeded, 3, ifelse(fulltime_goals == fulltime_goals_conceeded, sample(1, length(fulltime_goals), replace = TRUE), 0))) %>% 
  cbind(games_played) %>% 
                                                                                          ###calculate totals
  mutate(total_points_gained = cumsum(points_gained),
         total_fulltime_goals_scored = cumsum(fulltime_goals),
         total_fulltime_goals_conceeded = cumsum(fulltime_goals_conceeded),
         total_halftime_goals = cumsum(halftime_goals),
         total_halftime_goals_conceeded = cumsum(halftime_goals_conceeded),
         total_shots = cumsum(shots),
         total_opposition_shots = cumsum(opposition_shots),
         total_shots_on_target = cumsum(shots_on_target),
         total_opposition_shots_on_target = cumsum(opposition_shots_on_target),
         total_fouls = cumsum(fouls),
         total_opposition_fouls = cumsum(opposition_fouls),
         total_corners = cumsum(corners),
         total_opposition_corners = cumsum(opposition_corners),
         total_yellow_cards = cumsum(yellow_cards),
         total_opposition_yellow_cards = cumsum(opposition_yellow_cards),
         total_red_cards = cumsum(red_cards),
         total_opposition_red_cards = cumsum(opposition_red_cards)) %>% 
                                                                                          ###calcualte averages
   mutate(average_points_gained = total_points_gained/games_played,
         average_fulltime_goals_scored = total_fulltime_goals_scored/games_played,
         average_fulltime_goals_conceeded = total_fulltime_goals_conceeded/games_played,
         average_halftime_goals = total_halftime_goals/games_played,
         average_halftime_goals_conceeded = total_halftime_goals_conceeded/games_played,
         average_shots = total_shots/games_played,
         average_opposition_shots = total_opposition_shots/games_played,
         average_shots_on_target = total_shots_on_target/games_played,
         average_opposition_shots_on_target = total_opposition_shots_on_target/games_played,
         average_fouls = total_fouls/games_played,
         average_opposition_fouls = total_opposition_fouls/games_played,
         average_corners = total_corners/games_played,
         average_opposition_corners = total_opposition_corners/games_played,
         average_yellow_cards = total_yellow_cards/games_played,
         average_opposition_yellow_cards = total_opposition_yellow_cards/games_played,
         average_red_cards = total_red_cards/games_played,
         average_opposition_red_cards = total_opposition_red_cards/games_played) %>% 
                                                                                         ###calculate form
  mutate(points_gained_form = movavg(points_gained, 6),
         fulltime_goals_form = movavg(fulltime_goals, 6),
         fulltime_goals_conceeded_form = movavg(fulltime_goals_conceeded, 6),
         halftime_goals_form = movavg(halftime_goals, 6),
         halftime_goals_conceeded_form = movavg(halftime_goals_conceeded, 6),
         shots_form = movavg(shots, 6),
         opposition_shots_form = movavg(opposition_shots, 6),
         shots_on_target_form = movavg(shots_on_target, 6),
         opposition_shots_on_target_form = movavg(opposition_shots_on_target, 6),
         fouls_form = movavg(fouls, 6),
         opposition_fouls_form = movavg(opposition_fouls, 6),
         corners_form = movavg(corners, 6),
         opposition_corners_form = movavg(opposition_corners, 6),
         yellow_cards_form = movavg(yellow_cards, 6),
         opposition_yellow_cards_form = movavg(opposition_yellow_cards, 6),
         red_cards_form = movavg(red_cards, 6),
         opposition_red_cards_form = movavg(opposition_red_cards, 6))
## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

Everton

Everton <- rbind(Everton_Home, Everton_Away) %>% 
  arrange(Date) %>% 
  mutate(points_gained = ifelse(fulltime_goals > fulltime_goals_conceeded, 3, ifelse(fulltime_goals == fulltime_goals_conceeded, sample(1, length(fulltime_goals), replace = TRUE), 0))) %>% 
  cbind(games_played) %>% 
                                                                                          ###calculate totals
  mutate(total_points_gained = cumsum(points_gained),
         total_fulltime_goals_scored = cumsum(fulltime_goals),
         total_fulltime_goals_conceeded = cumsum(fulltime_goals_conceeded),
         total_halftime_goals = cumsum(halftime_goals),
         total_halftime_goals_conceeded = cumsum(halftime_goals_conceeded),
         total_shots = cumsum(shots),
         total_opposition_shots = cumsum(opposition_shots),
         total_shots_on_target = cumsum(shots_on_target),
         total_opposition_shots_on_target = cumsum(opposition_shots_on_target),
         total_fouls = cumsum(fouls),
         total_opposition_fouls = cumsum(opposition_fouls),
         total_corners = cumsum(corners),
         total_opposition_corners = cumsum(opposition_corners),
         total_yellow_cards = cumsum(yellow_cards),
         total_opposition_yellow_cards = cumsum(opposition_yellow_cards),
         total_red_cards = cumsum(red_cards),
         total_opposition_red_cards = cumsum(opposition_red_cards)) %>% 
                                                                                          ###calcualte averages
   mutate(average_points_gained = total_points_gained/games_played,
         average_fulltime_goals_scored = total_fulltime_goals_scored/games_played,
         average_fulltime_goals_conceeded = total_fulltime_goals_conceeded/games_played,
         average_halftime_goals = total_halftime_goals/games_played,
         average_halftime_goals_conceeded = total_halftime_goals_conceeded/games_played,
         average_shots = total_shots/games_played,
         average_opposition_shots = total_opposition_shots/games_played,
         average_shots_on_target = total_shots_on_target/games_played,
         average_opposition_shots_on_target = total_opposition_shots_on_target/games_played,
         average_fouls = total_fouls/games_played,
         average_opposition_fouls = total_opposition_fouls/games_played,
         average_corners = total_corners/games_played,
         average_opposition_corners = total_opposition_corners/games_played,
         average_yellow_cards = total_yellow_cards/games_played,
         average_opposition_yellow_cards = total_opposition_yellow_cards/games_played,
         average_red_cards = total_red_cards/games_played,
         average_opposition_red_cards = total_opposition_red_cards/games_played) %>% 
                                                                                         ###calculate form
  mutate(points_gained_form = movavg(points_gained, 6),
         fulltime_goals_form = movavg(fulltime_goals, 6),
         fulltime_goals_conceeded_form = movavg(fulltime_goals_conceeded, 6),
         halftime_goals_form = movavg(halftime_goals, 6),
         halftime_goals_conceeded_form = movavg(halftime_goals_conceeded, 6),
         shots_form = movavg(shots, 6),
         opposition_shots_form = movavg(opposition_shots, 6),
         shots_on_target_form = movavg(shots_on_target, 6),
         opposition_shots_on_target_form = movavg(opposition_shots_on_target, 6),
         fouls_form = movavg(fouls, 6),
         opposition_fouls_form = movavg(opposition_fouls, 6),
         corners_form = movavg(corners, 6),
         opposition_corners_form = movavg(opposition_corners, 6),
         yellow_cards_form = movavg(yellow_cards, 6),
         opposition_yellow_cards_form = movavg(opposition_yellow_cards, 6),
         red_cards_form = movavg(red_cards, 6),
         opposition_red_cards_form = movavg(opposition_red_cards, 6))
## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

Fulham

Fulham <- rbind(Fulham_Home, Fulham_Away) %>% 
  arrange(Date) %>% 
  mutate(points_gained = ifelse(fulltime_goals > fulltime_goals_conceeded, 3, ifelse(fulltime_goals == fulltime_goals_conceeded, sample(1, length(fulltime_goals), replace = TRUE), 0))) %>% 
  cbind(games_played) %>% 
                                                                                          ###calculate totals
  mutate(total_points_gained = cumsum(points_gained),
         total_fulltime_goals_scored = cumsum(fulltime_goals),
         total_fulltime_goals_conceeded = cumsum(fulltime_goals_conceeded),
         total_halftime_goals = cumsum(halftime_goals),
         total_halftime_goals_conceeded = cumsum(halftime_goals_conceeded),
         total_shots = cumsum(shots),
         total_opposition_shots = cumsum(opposition_shots),
         total_shots_on_target = cumsum(shots_on_target),
         total_opposition_shots_on_target = cumsum(opposition_shots_on_target),
         total_fouls = cumsum(fouls),
         total_opposition_fouls = cumsum(opposition_fouls),
         total_corners = cumsum(corners),
         total_opposition_corners = cumsum(opposition_corners),
         total_yellow_cards = cumsum(yellow_cards),
         total_opposition_yellow_cards = cumsum(opposition_yellow_cards),
         total_red_cards = cumsum(red_cards),
         total_opposition_red_cards = cumsum(opposition_red_cards)) %>% 
                                                                                          ###calcualte averages
   mutate(average_points_gained = total_points_gained/games_played,
         average_fulltime_goals_scored = total_fulltime_goals_scored/games_played,
         average_fulltime_goals_conceeded = total_fulltime_goals_conceeded/games_played,
         average_halftime_goals = total_halftime_goals/games_played,
         average_halftime_goals_conceeded = total_halftime_goals_conceeded/games_played,
         average_shots = total_shots/games_played,
         average_opposition_shots = total_opposition_shots/games_played,
         average_shots_on_target = total_shots_on_target/games_played,
         average_opposition_shots_on_target = total_opposition_shots_on_target/games_played,
         average_fouls = total_fouls/games_played,
         average_opposition_fouls = total_opposition_fouls/games_played,
         average_corners = total_corners/games_played,
         average_opposition_corners = total_opposition_corners/games_played,
         average_yellow_cards = total_yellow_cards/games_played,
         average_opposition_yellow_cards = total_opposition_yellow_cards/games_played,
         average_red_cards = total_red_cards/games_played,
         average_opposition_red_cards = total_opposition_red_cards/games_played) %>% 
                                                                                         ###calculate form
  mutate(points_gained_form = movavg(points_gained, 6),
         fulltime_goals_form = movavg(fulltime_goals, 6),
         fulltime_goals_conceeded_form = movavg(fulltime_goals_conceeded, 6),
         halftime_goals_form = movavg(halftime_goals, 6),
         halftime_goals_conceeded_form = movavg(halftime_goals_conceeded, 6),
         shots_form = movavg(shots, 6),
         opposition_shots_form = movavg(opposition_shots, 6),
         shots_on_target_form = movavg(shots_on_target, 6),
         opposition_shots_on_target_form = movavg(opposition_shots_on_target, 6),
         fouls_form = movavg(fouls, 6),
         opposition_fouls_form = movavg(opposition_fouls, 6),
         corners_form = movavg(corners, 6),
         opposition_corners_form = movavg(opposition_corners, 6),
         yellow_cards_form = movavg(yellow_cards, 6),
         opposition_yellow_cards_form = movavg(opposition_yellow_cards, 6),
         red_cards_form = movavg(red_cards, 6),
         opposition_red_cards_form = movavg(opposition_red_cards, 6))
## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

Huddersfield

Huddersfield <- rbind(Huddersfield_Home, Huddersfield_Away) %>% 
  arrange(Date) %>% 
  mutate(points_gained = ifelse(fulltime_goals > fulltime_goals_conceeded, 3, ifelse(fulltime_goals == fulltime_goals_conceeded, sample(1, length(fulltime_goals), replace = TRUE), 0))) %>% 
  cbind(games_played) %>% 
                                                                                          ###calculate totals
  mutate(total_points_gained = cumsum(points_gained),
         total_fulltime_goals_scored = cumsum(fulltime_goals),
         total_fulltime_goals_conceeded = cumsum(fulltime_goals_conceeded),
         total_halftime_goals = cumsum(halftime_goals),
         total_halftime_goals_conceeded = cumsum(halftime_goals_conceeded),
         total_shots = cumsum(shots),
         total_opposition_shots = cumsum(opposition_shots),
         total_shots_on_target = cumsum(shots_on_target),
         total_opposition_shots_on_target = cumsum(opposition_shots_on_target),
         total_fouls = cumsum(fouls),
         total_opposition_fouls = cumsum(opposition_fouls),
         total_corners = cumsum(corners),
         total_opposition_corners = cumsum(opposition_corners),
         total_yellow_cards = cumsum(yellow_cards),
         total_opposition_yellow_cards = cumsum(opposition_yellow_cards),
         total_red_cards = cumsum(red_cards),
         total_opposition_red_cards = cumsum(opposition_red_cards)) %>% 
                                                                                          ###calcualte averages
   mutate(average_points_gained = total_points_gained/games_played,
         average_fulltime_goals_scored = total_fulltime_goals_scored/games_played,
         average_fulltime_goals_conceeded = total_fulltime_goals_conceeded/games_played,
         average_halftime_goals = total_halftime_goals/games_played,
         average_halftime_goals_conceeded = total_halftime_goals_conceeded/games_played,
         average_shots = total_shots/games_played,
         average_opposition_shots = total_opposition_shots/games_played,
         average_shots_on_target = total_shots_on_target/games_played,
         average_opposition_shots_on_target = total_opposition_shots_on_target/games_played,
         average_fouls = total_fouls/games_played,
         average_opposition_fouls = total_opposition_fouls/games_played,
         average_corners = total_corners/games_played,
         average_opposition_corners = total_opposition_corners/games_played,
         average_yellow_cards = total_yellow_cards/games_played,
         average_opposition_yellow_cards = total_opposition_yellow_cards/games_played,
         average_red_cards = total_red_cards/games_played,
         average_opposition_red_cards = total_opposition_red_cards/games_played) %>% 
                                                                                         ###calculate form
  mutate(points_gained_form = movavg(points_gained, 6),
         fulltime_goals_form = movavg(fulltime_goals, 6),
         fulltime_goals_conceeded_form = movavg(fulltime_goals_conceeded, 6),
         halftime_goals_form = movavg(halftime_goals, 6),
         halftime_goals_conceeded_form = movavg(halftime_goals_conceeded, 6),
         shots_form = movavg(shots, 6),
         opposition_shots_form = movavg(opposition_shots, 6),
         shots_on_target_form = movavg(shots_on_target, 6),
         opposition_shots_on_target_form = movavg(opposition_shots_on_target, 6),
         fouls_form = movavg(fouls, 6),
         opposition_fouls_form = movavg(opposition_fouls, 6),
         corners_form = movavg(corners, 6),
         opposition_corners_form = movavg(opposition_corners, 6),
         yellow_cards_form = movavg(yellow_cards, 6),
         opposition_yellow_cards_form = movavg(opposition_yellow_cards, 6),
         red_cards_form = movavg(red_cards, 6),
         opposition_red_cards_form = movavg(opposition_red_cards, 6))
## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

Leicester

Leicester <- rbind(Leicester_Home, Leicester_Away) %>% 
  arrange(Date) %>% 
  mutate(points_gained = ifelse(fulltime_goals > fulltime_goals_conceeded, 3, ifelse(fulltime_goals == fulltime_goals_conceeded, sample(1, length(fulltime_goals), replace = TRUE), 0))) %>% 
  cbind(games_played) %>% 
                                                                                          ###calculate totals
  mutate(total_points_gained = cumsum(points_gained),
         total_fulltime_goals_scored = cumsum(fulltime_goals),
         total_fulltime_goals_conceeded = cumsum(fulltime_goals_conceeded),
         total_halftime_goals = cumsum(halftime_goals),
         total_halftime_goals_conceeded = cumsum(halftime_goals_conceeded),
         total_shots = cumsum(shots),
         total_opposition_shots = cumsum(opposition_shots),
         total_shots_on_target = cumsum(shots_on_target),
         total_opposition_shots_on_target = cumsum(opposition_shots_on_target),
         total_fouls = cumsum(fouls),
         total_opposition_fouls = cumsum(opposition_fouls),
         total_corners = cumsum(corners),
         total_opposition_corners = cumsum(opposition_corners),
         total_yellow_cards = cumsum(yellow_cards),
         total_opposition_yellow_cards = cumsum(opposition_yellow_cards),
         total_red_cards = cumsum(red_cards),
         total_opposition_red_cards = cumsum(opposition_red_cards)) %>% 
                                                                                          ###calcualte averages
   mutate(average_points_gained = total_points_gained/games_played,
         average_fulltime_goals_scored = total_fulltime_goals_scored/games_played,
         average_fulltime_goals_conceeded = total_fulltime_goals_conceeded/games_played,
         average_halftime_goals = total_halftime_goals/games_played,
         average_halftime_goals_conceeded = total_halftime_goals_conceeded/games_played,
         average_shots = total_shots/games_played,
         average_opposition_shots = total_opposition_shots/games_played,
         average_shots_on_target = total_shots_on_target/games_played,
         average_opposition_shots_on_target = total_opposition_shots_on_target/games_played,
         average_fouls = total_fouls/games_played,
         average_opposition_fouls = total_opposition_fouls/games_played,
         average_corners = total_corners/games_played,
         average_opposition_corners = total_opposition_corners/games_played,
         average_yellow_cards = total_yellow_cards/games_played,
         average_opposition_yellow_cards = total_opposition_yellow_cards/games_played,
         average_red_cards = total_red_cards/games_played,
         average_opposition_red_cards = total_opposition_red_cards/games_played) %>% 
                                                                                         ###calculate form
  mutate(points_gained_form = movavg(points_gained, 6),
         fulltime_goals_form = movavg(fulltime_goals, 6),
         fulltime_goals_conceeded_form = movavg(fulltime_goals_conceeded, 6),
         halftime_goals_form = movavg(halftime_goals, 6),
         halftime_goals_conceeded_form = movavg(halftime_goals_conceeded, 6),
         shots_form = movavg(shots, 6),
         opposition_shots_form = movavg(opposition_shots, 6),
         shots_on_target_form = movavg(shots_on_target, 6),
         opposition_shots_on_target_form = movavg(opposition_shots_on_target, 6),
         fouls_form = movavg(fouls, 6),
         opposition_fouls_form = movavg(opposition_fouls, 6),
         corners_form = movavg(corners, 6),
         opposition_corners_form = movavg(opposition_corners, 6),
         yellow_cards_form = movavg(yellow_cards, 6),
         opposition_yellow_cards_form = movavg(opposition_yellow_cards, 6),
         red_cards_form = movavg(red_cards, 6),
         opposition_red_cards_form = movavg(opposition_red_cards, 6))
## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

Liverpool

Liverpool <- rbind(Liverpool_Home, Liverpool_Away) %>% 
  arrange(Date) %>% 
  mutate(points_gained = ifelse(fulltime_goals > fulltime_goals_conceeded, 3, ifelse(fulltime_goals == fulltime_goals_conceeded, sample(1, length(fulltime_goals), replace = TRUE), 0))) %>% 
  cbind(games_played) %>% 
                                                                                          ###calculate totals
  mutate(total_points_gained = cumsum(points_gained),
         total_fulltime_goals_scored = cumsum(fulltime_goals),
         total_fulltime_goals_conceeded = cumsum(fulltime_goals_conceeded),
         total_halftime_goals = cumsum(halftime_goals),
         total_halftime_goals_conceeded = cumsum(halftime_goals_conceeded),
         total_shots = cumsum(shots),
         total_opposition_shots = cumsum(opposition_shots),
         total_shots_on_target = cumsum(shots_on_target),
         total_opposition_shots_on_target = cumsum(opposition_shots_on_target),
         total_fouls = cumsum(fouls),
         total_opposition_fouls = cumsum(opposition_fouls),
         total_corners = cumsum(corners),
         total_opposition_corners = cumsum(opposition_corners),
         total_yellow_cards = cumsum(yellow_cards),
         total_opposition_yellow_cards = cumsum(opposition_yellow_cards),
         total_red_cards = cumsum(red_cards),
         total_opposition_red_cards = cumsum(opposition_red_cards)) %>% 
                                                                                          ###calcualte averages
   mutate(average_points_gained = total_points_gained/games_played,
         average_fulltime_goals_scored = total_fulltime_goals_scored/games_played,
         average_fulltime_goals_conceeded = total_fulltime_goals_conceeded/games_played,
         average_halftime_goals = total_halftime_goals/games_played,
         average_halftime_goals_conceeded = total_halftime_goals_conceeded/games_played,
         average_shots = total_shots/games_played,
         average_opposition_shots = total_opposition_shots/games_played,
         average_shots_on_target = total_shots_on_target/games_played,
         average_opposition_shots_on_target = total_opposition_shots_on_target/games_played,
         average_fouls = total_fouls/games_played,
         average_opposition_fouls = total_opposition_fouls/games_played,
         average_corners = total_corners/games_played,
         average_opposition_corners = total_opposition_corners/games_played,
         average_yellow_cards = total_yellow_cards/games_played,
         average_opposition_yellow_cards = total_opposition_yellow_cards/games_played,
         average_red_cards = total_red_cards/games_played,
         average_opposition_red_cards = total_opposition_red_cards/games_played) %>% 
                                                                                         ###calculate form
  mutate(points_gained_form = movavg(points_gained, 6),
         fulltime_goals_form = movavg(fulltime_goals, 6),
         fulltime_goals_conceeded_form = movavg(fulltime_goals_conceeded, 6),
         halftime_goals_form = movavg(halftime_goals, 6),
         halftime_goals_conceeded_form = movavg(halftime_goals_conceeded, 6),
         shots_form = movavg(shots, 6),
         opposition_shots_form = movavg(opposition_shots, 6),
         shots_on_target_form = movavg(shots_on_target, 6),
         opposition_shots_on_target_form = movavg(opposition_shots_on_target, 6),
         fouls_form = movavg(fouls, 6),
         opposition_fouls_form = movavg(opposition_fouls, 6),
         corners_form = movavg(corners, 6),
         opposition_corners_form = movavg(opposition_corners, 6),
         yellow_cards_form = movavg(yellow_cards, 6),
         opposition_yellow_cards_form = movavg(opposition_yellow_cards, 6),
         red_cards_form = movavg(red_cards, 6),
         opposition_red_cards_form = movavg(opposition_red_cards, 6))
## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

Man City

Man_City <- rbind(Man_City_Home, Man_City_Away) %>% 
  arrange(Date) %>% 
  mutate(points_gained = ifelse(fulltime_goals > fulltime_goals_conceeded, 3, ifelse(fulltime_goals == fulltime_goals_conceeded, sample(1, length(fulltime_goals), replace = TRUE), 0))) %>% 
  cbind(games_played) %>% 
                                                                                          ###calculate totals
  mutate(total_points_gained = cumsum(points_gained),
         total_fulltime_goals_scored = cumsum(fulltime_goals),
         total_fulltime_goals_conceeded = cumsum(fulltime_goals_conceeded),
         total_halftime_goals = cumsum(halftime_goals),
         total_halftime_goals_conceeded = cumsum(halftime_goals_conceeded),
         total_shots = cumsum(shots),
         total_opposition_shots = cumsum(opposition_shots),
         total_shots_on_target = cumsum(shots_on_target),
         total_opposition_shots_on_target = cumsum(opposition_shots_on_target),
         total_fouls = cumsum(fouls),
         total_opposition_fouls = cumsum(opposition_fouls),
         total_corners = cumsum(corners),
         total_opposition_corners = cumsum(opposition_corners),
         total_yellow_cards = cumsum(yellow_cards),
         total_opposition_yellow_cards = cumsum(opposition_yellow_cards),
         total_red_cards = cumsum(red_cards),
         total_opposition_red_cards = cumsum(opposition_red_cards)) %>% 
                                                                                          ###calcualte averages
   mutate(average_points_gained = total_points_gained/games_played,
         average_fulltime_goals_scored = total_fulltime_goals_scored/games_played,
         average_fulltime_goals_conceeded = total_fulltime_goals_conceeded/games_played,
         average_halftime_goals = total_halftime_goals/games_played,
         average_halftime_goals_conceeded = total_halftime_goals_conceeded/games_played,
         average_shots = total_shots/games_played,
         average_opposition_shots = total_opposition_shots/games_played,
         average_shots_on_target = total_shots_on_target/games_played,
         average_opposition_shots_on_target = total_opposition_shots_on_target/games_played,
         average_fouls = total_fouls/games_played,
         average_opposition_fouls = total_opposition_fouls/games_played,
         average_corners = total_corners/games_played,
         average_opposition_corners = total_opposition_corners/games_played,
         average_yellow_cards = total_yellow_cards/games_played,
         average_opposition_yellow_cards = total_opposition_yellow_cards/games_played,
         average_red_cards = total_red_cards/games_played,
         average_opposition_red_cards = total_opposition_red_cards/games_played) %>% 
                                                                                         ###calculate form
  mutate(points_gained_form = movavg(points_gained, 6),
         fulltime_goals_form = movavg(fulltime_goals, 6),
         fulltime_goals_conceeded_form = movavg(fulltime_goals_conceeded, 6),
         halftime_goals_form = movavg(halftime_goals, 6),
         halftime_goals_conceeded_form = movavg(halftime_goals_conceeded, 6),
         shots_form = movavg(shots, 6),
         opposition_shots_form = movavg(opposition_shots, 6),
         shots_on_target_form = movavg(shots_on_target, 6),
         opposition_shots_on_target_form = movavg(opposition_shots_on_target, 6),
         fouls_form = movavg(fouls, 6),
         opposition_fouls_form = movavg(opposition_fouls, 6),
         corners_form = movavg(corners, 6),
         opposition_corners_form = movavg(opposition_corners, 6),
         yellow_cards_form = movavg(yellow_cards, 6),
         opposition_yellow_cards_form = movavg(opposition_yellow_cards, 6),
         red_cards_form = movavg(red_cards, 6),
         opposition_red_cards_form = movavg(opposition_red_cards, 6))
## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

Man United

Man_United <- rbind(Man_United_Home, Man_United_Away) %>% 
  arrange(Date) %>% 
  mutate(points_gained = ifelse(fulltime_goals > fulltime_goals_conceeded, 3, ifelse(fulltime_goals == fulltime_goals_conceeded, sample(1, length(fulltime_goals), replace = TRUE), 0))) %>% 
  cbind(games_played) %>% 
                                                                                          ###calculate totals
  mutate(total_points_gained = cumsum(points_gained),
         total_fulltime_goals_scored = cumsum(fulltime_goals),
         total_fulltime_goals_conceeded = cumsum(fulltime_goals_conceeded),
         total_halftime_goals = cumsum(halftime_goals),
         total_halftime_goals_conceeded = cumsum(halftime_goals_conceeded),
         total_shots = cumsum(shots),
         total_opposition_shots = cumsum(opposition_shots),
         total_shots_on_target = cumsum(shots_on_target),
         total_opposition_shots_on_target = cumsum(opposition_shots_on_target),
         total_fouls = cumsum(fouls),
         total_opposition_fouls = cumsum(opposition_fouls),
         total_corners = cumsum(corners),
         total_opposition_corners = cumsum(opposition_corners),
         total_yellow_cards = cumsum(yellow_cards),
         total_opposition_yellow_cards = cumsum(opposition_yellow_cards),
         total_red_cards = cumsum(red_cards),
         total_opposition_red_cards = cumsum(opposition_red_cards)) %>% 
                                                                                          ###calcualte averages
   mutate(average_points_gained = total_points_gained/games_played,
         average_fulltime_goals_scored = total_fulltime_goals_scored/games_played,
         average_fulltime_goals_conceeded = total_fulltime_goals_conceeded/games_played,
         average_halftime_goals = total_halftime_goals/games_played,
         average_halftime_goals_conceeded = total_halftime_goals_conceeded/games_played,
         average_shots = total_shots/games_played,
         average_opposition_shots = total_opposition_shots/games_played,
         average_shots_on_target = total_shots_on_target/games_played,
         average_opposition_shots_on_target = total_opposition_shots_on_target/games_played,
         average_fouls = total_fouls/games_played,
         average_opposition_fouls = total_opposition_fouls/games_played,
         average_corners = total_corners/games_played,
         average_opposition_corners = total_opposition_corners/games_played,
         average_yellow_cards = total_yellow_cards/games_played,
         average_opposition_yellow_cards = total_opposition_yellow_cards/games_played,
         average_red_cards = total_red_cards/games_played,
         average_opposition_red_cards = total_opposition_red_cards/games_played) %>% 
                                                                                         ###calculate form
  mutate(points_gained_form = movavg(points_gained, 6),
         fulltime_goals_form = movavg(fulltime_goals, 6),
         fulltime_goals_conceeded_form = movavg(fulltime_goals_conceeded, 6),
         halftime_goals_form = movavg(halftime_goals, 6),
         halftime_goals_conceeded_form = movavg(halftime_goals_conceeded, 6),
         shots_form = movavg(shots, 6),
         opposition_shots_form = movavg(opposition_shots, 6),
         shots_on_target_form = movavg(shots_on_target, 6),
         opposition_shots_on_target_form = movavg(opposition_shots_on_target, 6),
         fouls_form = movavg(fouls, 6),
         opposition_fouls_form = movavg(opposition_fouls, 6),
         corners_form = movavg(corners, 6),
         opposition_corners_form = movavg(opposition_corners, 6),
         yellow_cards_form = movavg(yellow_cards, 6),
         opposition_yellow_cards_form = movavg(opposition_yellow_cards, 6),
         red_cards_form = movavg(red_cards, 6),
         opposition_red_cards_form = movavg(opposition_red_cards, 6))
## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

Newcastle

Newcastle <- rbind(Newcastle_Home, Newcastle_Away) %>% 
  arrange(Date) %>% 
  mutate(points_gained = ifelse(fulltime_goals > fulltime_goals_conceeded, 3, ifelse(fulltime_goals == fulltime_goals_conceeded, sample(1, length(fulltime_goals), replace = TRUE), 0))) %>% 
  cbind(games_played) %>% 
                                                                                          ###calculate totals
  mutate(total_points_gained = cumsum(points_gained),
         total_fulltime_goals_scored = cumsum(fulltime_goals),
         total_fulltime_goals_conceeded = cumsum(fulltime_goals_conceeded),
         total_halftime_goals = cumsum(halftime_goals),
         total_halftime_goals_conceeded = cumsum(halftime_goals_conceeded),
         total_shots = cumsum(shots),
         total_opposition_shots = cumsum(opposition_shots),
         total_shots_on_target = cumsum(shots_on_target),
         total_opposition_shots_on_target = cumsum(opposition_shots_on_target),
         total_fouls = cumsum(fouls),
         total_opposition_fouls = cumsum(opposition_fouls),
         total_corners = cumsum(corners),
         total_opposition_corners = cumsum(opposition_corners),
         total_yellow_cards = cumsum(yellow_cards),
         total_opposition_yellow_cards = cumsum(opposition_yellow_cards),
         total_red_cards = cumsum(red_cards),
         total_opposition_red_cards = cumsum(opposition_red_cards)) %>% 
                                                                                          ###calcualte averages
   mutate(average_points_gained = total_points_gained/games_played,
         average_fulltime_goals_scored = total_fulltime_goals_scored/games_played,
         average_fulltime_goals_conceeded = total_fulltime_goals_conceeded/games_played,
         average_halftime_goals = total_halftime_goals/games_played,
         average_halftime_goals_conceeded = total_halftime_goals_conceeded/games_played,
         average_shots = total_shots/games_played,
         average_opposition_shots = total_opposition_shots/games_played,
         average_shots_on_target = total_shots_on_target/games_played,
         average_opposition_shots_on_target = total_opposition_shots_on_target/games_played,
         average_fouls = total_fouls/games_played,
         average_opposition_fouls = total_opposition_fouls/games_played,
         average_corners = total_corners/games_played,
         average_opposition_corners = total_opposition_corners/games_played,
         average_yellow_cards = total_yellow_cards/games_played,
         average_opposition_yellow_cards = total_opposition_yellow_cards/games_played,
         average_red_cards = total_red_cards/games_played,
         average_opposition_red_cards = total_opposition_red_cards/games_played) %>% 
                                                                                         ###calculate form
  mutate(points_gained_form = movavg(points_gained, 6),
         fulltime_goals_form = movavg(fulltime_goals, 6),
         fulltime_goals_conceeded_form = movavg(fulltime_goals_conceeded, 6),
         halftime_goals_form = movavg(halftime_goals, 6),
         halftime_goals_conceeded_form = movavg(halftime_goals_conceeded, 6),
         shots_form = movavg(shots, 6),
         opposition_shots_form = movavg(opposition_shots, 6),
         shots_on_target_form = movavg(shots_on_target, 6),
         opposition_shots_on_target_form = movavg(opposition_shots_on_target, 6),
         fouls_form = movavg(fouls, 6),
         opposition_fouls_form = movavg(opposition_fouls, 6),
         corners_form = movavg(corners, 6),
         opposition_corners_form = movavg(opposition_corners, 6),
         yellow_cards_form = movavg(yellow_cards, 6),
         opposition_yellow_cards_form = movavg(opposition_yellow_cards, 6),
         red_cards_form = movavg(red_cards, 6),
         opposition_red_cards_form = movavg(opposition_red_cards, 6))
## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

Southampton

Southampton <- rbind(Southampton_Home, Southampton_Away) %>% 
  arrange(Date) %>% 
  mutate(points_gained = ifelse(fulltime_goals > fulltime_goals_conceeded, 3, ifelse(fulltime_goals == fulltime_goals_conceeded, sample(1, length(fulltime_goals), replace = TRUE), 0))) %>% 
  cbind(games_played) %>% 
                                                                                          ###calculate totals
  mutate(total_points_gained = cumsum(points_gained),
         total_fulltime_goals_scored = cumsum(fulltime_goals),
         total_fulltime_goals_conceeded = cumsum(fulltime_goals_conceeded),
         total_halftime_goals = cumsum(halftime_goals),
         total_halftime_goals_conceeded = cumsum(halftime_goals_conceeded),
         total_shots = cumsum(shots),
         total_opposition_shots = cumsum(opposition_shots),
         total_shots_on_target = cumsum(shots_on_target),
         total_opposition_shots_on_target = cumsum(opposition_shots_on_target),
         total_fouls = cumsum(fouls),
         total_opposition_fouls = cumsum(opposition_fouls),
         total_corners = cumsum(corners),
         total_opposition_corners = cumsum(opposition_corners),
         total_yellow_cards = cumsum(yellow_cards),
         total_opposition_yellow_cards = cumsum(opposition_yellow_cards),
         total_red_cards = cumsum(red_cards),
         total_opposition_red_cards = cumsum(opposition_red_cards)) %>% 
                                                                                          ###calcualte averages
   mutate(average_points_gained = total_points_gained/games_played,
         average_fulltime_goals_scored = total_fulltime_goals_scored/games_played,
         average_fulltime_goals_conceeded = total_fulltime_goals_conceeded/games_played,
         average_halftime_goals = total_halftime_goals/games_played,
         average_halftime_goals_conceeded = total_halftime_goals_conceeded/games_played,
         average_shots = total_shots/games_played,
         average_opposition_shots = total_opposition_shots/games_played,
         average_shots_on_target = total_shots_on_target/games_played,
         average_opposition_shots_on_target = total_opposition_shots_on_target/games_played,
         average_fouls = total_fouls/games_played,
         average_opposition_fouls = total_opposition_fouls/games_played,
         average_corners = total_corners/games_played,
         average_opposition_corners = total_opposition_corners/games_played,
         average_yellow_cards = total_yellow_cards/games_played,
         average_opposition_yellow_cards = total_opposition_yellow_cards/games_played,
         average_red_cards = total_red_cards/games_played,
         average_opposition_red_cards = total_opposition_red_cards/games_played) %>% 
                                                                                         ###calculate form
  mutate(points_gained_form = movavg(points_gained, 6),
         fulltime_goals_form = movavg(fulltime_goals, 6),
         fulltime_goals_conceeded_form = movavg(fulltime_goals_conceeded, 6),
         halftime_goals_form = movavg(halftime_goals, 6),
         halftime_goals_conceeded_form = movavg(halftime_goals_conceeded, 6),
         shots_form = movavg(shots, 6),
         opposition_shots_form = movavg(opposition_shots, 6),
         shots_on_target_form = movavg(shots_on_target, 6),
         opposition_shots_on_target_form = movavg(opposition_shots_on_target, 6),
         fouls_form = movavg(fouls, 6),
         opposition_fouls_form = movavg(opposition_fouls, 6),
         corners_form = movavg(corners, 6),
         opposition_corners_form = movavg(opposition_corners, 6),
         yellow_cards_form = movavg(yellow_cards, 6),
         opposition_yellow_cards_form = movavg(opposition_yellow_cards, 6),
         red_cards_form = movavg(red_cards, 6),
         opposition_red_cards_form = movavg(opposition_red_cards, 6))
## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

Tottenham

Tottenham <- rbind(Tottenham_Home, Tottenham_Away) %>% 
  arrange(Date) %>% 
  mutate(points_gained = ifelse(fulltime_goals > fulltime_goals_conceeded, 3, ifelse(fulltime_goals == fulltime_goals_conceeded, sample(1, length(fulltime_goals), replace = TRUE), 0))) %>% 
  cbind(games_played) %>% 
                                                                                          ###calculate totals
  mutate(total_points_gained = cumsum(points_gained),
         total_fulltime_goals_scored = cumsum(fulltime_goals),
         total_fulltime_goals_conceeded = cumsum(fulltime_goals_conceeded),
         total_halftime_goals = cumsum(halftime_goals),
         total_halftime_goals_conceeded = cumsum(halftime_goals_conceeded),
         total_shots = cumsum(shots),
         total_opposition_shots = cumsum(opposition_shots),
         total_shots_on_target = cumsum(shots_on_target),
         total_opposition_shots_on_target = cumsum(opposition_shots_on_target),
         total_fouls = cumsum(fouls),
         total_opposition_fouls = cumsum(opposition_fouls),
         total_corners = cumsum(corners),
         total_opposition_corners = cumsum(opposition_corners),
         total_yellow_cards = cumsum(yellow_cards),
         total_opposition_yellow_cards = cumsum(opposition_yellow_cards),
         total_red_cards = cumsum(red_cards),
         total_opposition_red_cards = cumsum(opposition_red_cards)) %>% 
                                                                                          ###calcualte averages
   mutate(average_points_gained = total_points_gained/games_played,
         average_fulltime_goals_scored = total_fulltime_goals_scored/games_played,
         average_fulltime_goals_conceeded = total_fulltime_goals_conceeded/games_played,
         average_halftime_goals = total_halftime_goals/games_played,
         average_halftime_goals_conceeded = total_halftime_goals_conceeded/games_played,
         average_shots = total_shots/games_played,
         average_opposition_shots = total_opposition_shots/games_played,
         average_shots_on_target = total_shots_on_target/games_played,
         average_opposition_shots_on_target = total_opposition_shots_on_target/games_played,
         average_fouls = total_fouls/games_played,
         average_opposition_fouls = total_opposition_fouls/games_played,
         average_corners = total_corners/games_played,
         average_opposition_corners = total_opposition_corners/games_played,
         average_yellow_cards = total_yellow_cards/games_played,
         average_opposition_yellow_cards = total_opposition_yellow_cards/games_played,
         average_red_cards = total_red_cards/games_played,
         average_opposition_red_cards = total_opposition_red_cards/games_played) %>% 
                                                                                         ###calculate form
  mutate(points_gained_form = movavg(points_gained, 6),
         fulltime_goals_form = movavg(fulltime_goals, 6),
         fulltime_goals_conceeded_form = movavg(fulltime_goals_conceeded, 6),
         halftime_goals_form = movavg(halftime_goals, 6),
         halftime_goals_conceeded_form = movavg(halftime_goals_conceeded, 6),
         shots_form = movavg(shots, 6),
         opposition_shots_form = movavg(opposition_shots, 6),
         shots_on_target_form = movavg(shots_on_target, 6),
         opposition_shots_on_target_form = movavg(opposition_shots_on_target, 6),
         fouls_form = movavg(fouls, 6),
         opposition_fouls_form = movavg(opposition_fouls, 6),
         corners_form = movavg(corners, 6),
         opposition_corners_form = movavg(opposition_corners, 6),
         yellow_cards_form = movavg(yellow_cards, 6),
         opposition_yellow_cards_form = movavg(opposition_yellow_cards, 6),
         red_cards_form = movavg(red_cards, 6),
         opposition_red_cards_form = movavg(opposition_red_cards, 6))
## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

Watford

Watford <- rbind(Watford_Home, Watford_Away) %>% 
  arrange(Date) %>% 
  mutate(points_gained = ifelse(fulltime_goals > fulltime_goals_conceeded, 3, ifelse(fulltime_goals == fulltime_goals_conceeded, sample(1, length(fulltime_goals), replace = TRUE), 0))) %>% 
  cbind(games_played) %>% 
                                                                                          ###calculate totals
  mutate(total_points_gained = cumsum(points_gained),
         total_fulltime_goals_scored = cumsum(fulltime_goals),
         total_fulltime_goals_conceeded = cumsum(fulltime_goals_conceeded),
         total_halftime_goals = cumsum(halftime_goals),
         total_halftime_goals_conceeded = cumsum(halftime_goals_conceeded),
         total_shots = cumsum(shots),
         total_opposition_shots = cumsum(opposition_shots),
         total_shots_on_target = cumsum(shots_on_target),
         total_opposition_shots_on_target = cumsum(opposition_shots_on_target),
         total_fouls = cumsum(fouls),
         total_opposition_fouls = cumsum(opposition_fouls),
         total_corners = cumsum(corners),
         total_opposition_corners = cumsum(opposition_corners),
         total_yellow_cards = cumsum(yellow_cards),
         total_opposition_yellow_cards = cumsum(opposition_yellow_cards),
         total_red_cards = cumsum(red_cards),
         total_opposition_red_cards = cumsum(opposition_red_cards)) %>% 
                                                                                          ###calcualte averages
   mutate(average_points_gained = total_points_gained/games_played,
         average_fulltime_goals_scored = total_fulltime_goals_scored/games_played,
         average_fulltime_goals_conceeded = total_fulltime_goals_conceeded/games_played,
         average_halftime_goals = total_halftime_goals/games_played,
         average_halftime_goals_conceeded = total_halftime_goals_conceeded/games_played,
         average_shots = total_shots/games_played,
         average_opposition_shots = total_opposition_shots/games_played,
         average_shots_on_target = total_shots_on_target/games_played,
         average_opposition_shots_on_target = total_opposition_shots_on_target/games_played,
         average_fouls = total_fouls/games_played,
         average_opposition_fouls = total_opposition_fouls/games_played,
         average_corners = total_corners/games_played,
         average_opposition_corners = total_opposition_corners/games_played,
         average_yellow_cards = total_yellow_cards/games_played,
         average_opposition_yellow_cards = total_opposition_yellow_cards/games_played,
         average_red_cards = total_red_cards/games_played,
         average_opposition_red_cards = total_opposition_red_cards/games_played) %>% 
                                                                                         ###calculate form
  mutate(points_gained_form = movavg(points_gained, 6),
         fulltime_goals_form = movavg(fulltime_goals, 6),
         fulltime_goals_conceeded_form = movavg(fulltime_goals_conceeded, 6),
         halftime_goals_form = movavg(halftime_goals, 6),
         halftime_goals_conceeded_form = movavg(halftime_goals_conceeded, 6),
         shots_form = movavg(shots, 6),
         opposition_shots_form = movavg(opposition_shots, 6),
         shots_on_target_form = movavg(shots_on_target, 6),
         opposition_shots_on_target_form = movavg(opposition_shots_on_target, 6),
         fouls_form = movavg(fouls, 6),
         opposition_fouls_form = movavg(opposition_fouls, 6),
         corners_form = movavg(corners, 6),
         opposition_corners_form = movavg(opposition_corners, 6),
         yellow_cards_form = movavg(yellow_cards, 6),
         opposition_yellow_cards_form = movavg(opposition_yellow_cards, 6),
         red_cards_form = movavg(red_cards, 6),
         opposition_red_cards_form = movavg(opposition_red_cards, 6))
## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

West Ham

West_Ham <- rbind(West_Ham_Home, West_Ham_Away) %>% 
  arrange(Date) %>% 
  mutate(points_gained = ifelse(fulltime_goals > fulltime_goals_conceeded, 3, ifelse(fulltime_goals == fulltime_goals_conceeded, sample(1, length(fulltime_goals), replace = TRUE), 0))) %>% 
  cbind(games_played) %>% 
                                                                                          ###calculate totals
  mutate(total_points_gained = cumsum(points_gained),
         total_fulltime_goals_scored = cumsum(fulltime_goals),
         total_fulltime_goals_conceeded = cumsum(fulltime_goals_conceeded),
         total_halftime_goals = cumsum(halftime_goals),
         total_halftime_goals_conceeded = cumsum(halftime_goals_conceeded),
         total_shots = cumsum(shots),
         total_opposition_shots = cumsum(opposition_shots),
         total_shots_on_target = cumsum(shots_on_target),
         total_opposition_shots_on_target = cumsum(opposition_shots_on_target),
         total_fouls = cumsum(fouls),
         total_opposition_fouls = cumsum(opposition_fouls),
         total_corners = cumsum(corners),
         total_opposition_corners = cumsum(opposition_corners),
         total_yellow_cards = cumsum(yellow_cards),
         total_opposition_yellow_cards = cumsum(opposition_yellow_cards),
         total_red_cards = cumsum(red_cards),
         total_opposition_red_cards = cumsum(opposition_red_cards)) %>% 
                                                                                          ###calcualte averages
   mutate(average_points_gained = total_points_gained/games_played,
         average_fulltime_goals_scored = total_fulltime_goals_scored/games_played,
         average_fulltime_goals_conceeded = total_fulltime_goals_conceeded/games_played,
         average_halftime_goals = total_halftime_goals/games_played,
         average_halftime_goals_conceeded = total_halftime_goals_conceeded/games_played,
         average_shots = total_shots/games_played,
         average_opposition_shots = total_opposition_shots/games_played,
         average_shots_on_target = total_shots_on_target/games_played,
         average_opposition_shots_on_target = total_opposition_shots_on_target/games_played,
         average_fouls = total_fouls/games_played,
         average_opposition_fouls = total_opposition_fouls/games_played,
         average_corners = total_corners/games_played,
         average_opposition_corners = total_opposition_corners/games_played,
         average_yellow_cards = total_yellow_cards/games_played,
         average_opposition_yellow_cards = total_opposition_yellow_cards/games_played,
         average_red_cards = total_red_cards/games_played,
         average_opposition_red_cards = total_opposition_red_cards/games_played) %>% 
                                                                                         ###calculate form
  mutate(points_gained_form = movavg(points_gained, 6),
         fulltime_goals_form = movavg(fulltime_goals, 6),
         fulltime_goals_conceeded_form = movavg(fulltime_goals_conceeded, 6),
         halftime_goals_form = movavg(halftime_goals, 6),
         halftime_goals_conceeded_form = movavg(halftime_goals_conceeded, 6),
         shots_form = movavg(shots, 6),
         opposition_shots_form = movavg(opposition_shots, 6),
         shots_on_target_form = movavg(shots_on_target, 6),
         opposition_shots_on_target_form = movavg(opposition_shots_on_target, 6),
         fouls_form = movavg(fouls, 6),
         opposition_fouls_form = movavg(opposition_fouls, 6),
         corners_form = movavg(corners, 6),
         opposition_corners_form = movavg(opposition_corners, 6),
         yellow_cards_form = movavg(yellow_cards, 6),
         opposition_yellow_cards_form = movavg(opposition_yellow_cards, 6),
         red_cards_form = movavg(red_cards, 6),
         opposition_red_cards_form = movavg(opposition_red_cards, 6))
## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

Wolves

Wolves <- rbind(Wolves_Home, Wolves_Away) %>% 
  arrange(Date) %>% 
  mutate(points_gained = ifelse(fulltime_goals > fulltime_goals_conceeded, 3, ifelse(fulltime_goals == fulltime_goals_conceeded, sample(1, length(fulltime_goals), replace = TRUE), 0))) %>% 
  cbind(games_played) %>% 
                                                                                          ###calculate totals
  mutate(total_points_gained = cumsum(points_gained),
         total_fulltime_goals_scored = cumsum(fulltime_goals),
         total_fulltime_goals_conceeded = cumsum(fulltime_goals_conceeded),
         total_halftime_goals = cumsum(halftime_goals),
         total_halftime_goals_conceeded = cumsum(halftime_goals_conceeded),
         total_shots = cumsum(shots),
         total_opposition_shots = cumsum(opposition_shots),
         total_shots_on_target = cumsum(shots_on_target),
         total_opposition_shots_on_target = cumsum(opposition_shots_on_target),
         total_fouls = cumsum(fouls),
         total_opposition_fouls = cumsum(opposition_fouls),
         total_corners = cumsum(corners),
         total_opposition_corners = cumsum(opposition_corners),
         total_yellow_cards = cumsum(yellow_cards),
         total_opposition_yellow_cards = cumsum(opposition_yellow_cards),
         total_red_cards = cumsum(red_cards),
         total_opposition_red_cards = cumsum(opposition_red_cards)) %>% 
                                                                                          ###calcualte averages
   mutate(average_points_gained = total_points_gained/games_played,
         average_fulltime_goals_scored = total_fulltime_goals_scored/games_played,
         average_fulltime_goals_conceeded = total_fulltime_goals_conceeded/games_played,
         average_halftime_goals = total_halftime_goals/games_played,
         average_halftime_goals_conceeded = total_halftime_goals_conceeded/games_played,
         average_shots = total_shots/games_played,
         average_opposition_shots = total_opposition_shots/games_played,
         average_shots_on_target = total_shots_on_target/games_played,
         average_opposition_shots_on_target = total_opposition_shots_on_target/games_played,
         average_fouls = total_fouls/games_played,
         average_opposition_fouls = total_opposition_fouls/games_played,
         average_corners = total_corners/games_played,
         average_opposition_corners = total_opposition_corners/games_played,
         average_yellow_cards = total_yellow_cards/games_played,
         average_opposition_yellow_cards = total_opposition_yellow_cards/games_played,
         average_red_cards = total_red_cards/games_played,
         average_opposition_red_cards = total_opposition_red_cards/games_played) %>% 
                                                                                         ###calculate form
  mutate(points_gained_form = movavg(points_gained, 6),
         fulltime_goals_form = movavg(fulltime_goals, 6),
         fulltime_goals_conceeded_form = movavg(fulltime_goals_conceeded, 6),
         halftime_goals_form = movavg(halftime_goals, 6),
         halftime_goals_conceeded_form = movavg(halftime_goals_conceeded, 6),
         shots_form = movavg(shots, 6),
         opposition_shots_form = movavg(opposition_shots, 6),
         shots_on_target_form = movavg(shots_on_target, 6),
         opposition_shots_on_target_form = movavg(opposition_shots_on_target, 6),
         fouls_form = movavg(fouls, 6),
         opposition_fouls_form = movavg(opposition_fouls, 6),
         corners_form = movavg(corners, 6),
         opposition_corners_form = movavg(opposition_corners, 6),
         yellow_cards_form = movavg(yellow_cards, 6),
         opposition_yellow_cards_form = movavg(opposition_yellow_cards, 6),
         red_cards_form = movavg(red_cards, 6),
         opposition_red_cards_form = movavg(opposition_red_cards, 6))
## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

## Warning in if (type == "s") {: the condition has length > 1 and only the
## first element will be used

Once off write of all this data

write_csv(Arsenal, “Arsenal.csv”) write_csv(Bournemouth, “Bournemouth.csv”) write_csv(Brighton, “Brighton.csv”) write_csv(Burnley, “Burnley.csv”) write_csv(Cardiff, “Cardiff.csv”) write_csv(Chelsea, “Chelsea.csv”) write_csv(Crystal_Palace, “Crystal_Palace.csv”) write_csv(Everton, “Everton.csv”) write_csv(Fulham, “Fulham.csv”) write_csv(Huddersfield, “Huddersfield.csv”) write_csv(Leicester, “Leicester.csv”) write_csv(Liverpool, “Liverpool.csv”) write_csv(Man_City, “Man_City.csv”) write_csv(Man_United, “Man_United.csv”) write_csv(Newcastle, “Newcastle.csv”) write_csv(Southampton, “Southampton.csv”) write_csv(Tottenham, “Tottenham.csv”) write_csv(Watford, “Watford.csv”) write_csv(West_Ham, “West_Ham.csv”) write_csv(Wolves, “Wolves.csv”)

Final tidy of high level data

joined <- joined %>% 
  rename(last_season_position = Pos,
         last_season_won = W,
         last_season_drew = D,
         last_season_lost = L,
         last_season_goals_scored = GF,
         last_season_goals_conceeded = GA,
         last_season_points = Pts)

Once off write of this data

write_csv(joined, “joined.csv”)