IMPORT DATA

# load packages #
library(plyr)
library(dplyr)
library(readxl)
library(tidyr)
library(stringr)
library(writexl)
library(tibble)
library(knitr)

1. IMPORT DATA

Import raw data files, separate name file column and add game detail columns
# list all files with ext xlsx #
files <- list.files(path = "Data/Raw Data Women/Attack", pattern = "*.xlsx", full.names = T)

# import files from list (columns as characters) and bind to form one large database of games #
game <- sapply(files, read_excel, simplify = FALSE, col_types = "text") %>%
  bind_rows(.id = "id")

# separate file name column into match number, home and away team, numbers = number of words in file path (eg. Data, Raw, Data, Women, Attack) #
game <- separate(game, id, c("1", "2", "3", "4", "5", "Match", "Home_T", "vs", "Away_T"))

# separate row names from sportscode into team, gender and extras #
game <- separate(game, category, c("Team", "Gender", "A", "B", "C"))

# add new column with Opposition and match_number_team #
game <- mutate(game, Opposition = ifelse(game$Team == game$Home_T, game$Away_T, game$Home_T)) 

# add new column with match_number_team #
game <- unite(game, Match_Team, c(Match, Team), remove = FALSE)

game2 <- head(game)
knitr::kable(game2[, 8:17], caption = "Tidied Data")
Tidied Data
Home_T vs Away_T start time end time Team Gender A B C
Argentina v Belgium 00:00:00:00 00:00:31:20 Argentina W Attack NA NA
Argentina v Belgium 00:00:00:00 00:00:06:84 Argentina W D50 Att NA
Argentina v Belgium 00:00:00:00 00:00:31:20 Argentina W Established Attack NA
Argentina v Belgium 00:00:00:00 00:00:01:32 Argentina W Game Actions Att
Argentina v Belgium 00:00:00:00 00:00:30:64 Argentina W Passing Sequence NA
Argentina v Belgium 00:00:00:33 00:00:01:24 Argentina W Passing NA NA

2. SPLIT PER TEAM

Separate games per team to identify each teams game style
# create data frame per team #
game_list <- split(game, game$Team)

GAME ACTIONS

3. FILTER

Extract game action rows, select and rename descriptive columns
game_actions_f <- function(x){
# extract from all data (Game) rows referring to Game Actions and save as separate data frame (Game_Actions) #
game_actions <- filter(x, A == "Game")

# rename columns #
game_actions <- plyr::rename(game_actions, c("descriptors..." = "Game.Action", 
                                             "...7" = "Effect", 
                                             "...8" = "Match.Status", 
                                             "...9" = "Match.Location", 
                                             "...10" = "Quality.Opp", 
                                             "...11" = "Location", 
                                             "...12" = "Attack.Type"))

# remove unnecessary columns #
game_actions <- select(game_actions, Match_Team, Team, Opposition, Gender, Game.Action, Effect, Match.Status, Match.Location, Quality.Opp, Location, Attack.Type)

return(game_actions)
}

game_actions_list <- list()
for (i in seq_along(game_list)){
  game_actions_list[[i]] <- game_actions_f(game_list[[i]])
}
Extract game actions performed during established attacks
# filter for attack type #
game_actions_e_list <- list()
for (i in seq_along(game_actions_list)){
  game_actions_e_list[[i]] <- filter(game_actions_list[[i]], Attack.Type == "Established")
}
Extract game actions performed during counter attacks
# filter for attack type #
game_actions_c_list <- list()
for (i in seq_along(game_actions_list)){
  game_actions_c_list[[i]] <- filter(game_actions_list[[i]], Attack.Type == "Counter")
}

4. MOVEMENT EFFECTS PER GAME ACTION PER ATTACK TYPE PER LOCATION

Create table to identify movement effects when dribbling in AC in established attacks and convert to percentages for comparison
# AC Movement Effect Dribble Established #
de_ace_f <- function(x){
  if (any(grepl("AC Att", x$Location))){
# extract from Game_Actions data frame only rows referring to AC and Dribble and save as separate data frame #
de_ace <- filter(x, Location == "AC Att" & Game.Action == "Dribble")

# create table with Teams as rows and Movement Effects as columns #
de_ace <- table(de_ace$Team, de_ace$Effect)

# transform to percentages (% dribble movement effects used in AC) #
de_ace <- round((prop.table(de_ace) * 100), digits = 2)

# convert to data frame #
de_ace <- as.data.frame.matrix(de_ace)

# list of movement effects #
e <- c("Maintain", "Gain", "Penetrating", "Turnover")

# check to see if all data frames contain all columns, if not add column=0 #
de_ace[e[!(e %in% colnames(de_ace))]] = 0

# rename all columns with prefix D_AC_E #
colnames(de_ace) <- paste("D_AC_E", colnames(de_ace), sep = "_")


return(de_ace)
  } else {(de_ace <- setNames(as.data.frame(matrix(c(0, 0, 0, 0), ncol = 4, nrow = 1)), c("D_AC_E_Main", "D_AC_E_Gain","D_AC_E_Penetrating", "D_AC_E_Turnover")))
    
  return(de_ace)}
}


de_ace_list <- list()
for (i in seq_along(game_actions_e_list)){
  de_ace_list[[i]] <- de_ace_f(game_actions_e_list[[i]])
}
Create table to identify movement effects when passing in AC in established attacks and convert to percentages for comparison
# AC Movement Effect Pass Established #
pe_ace_f <- function(x){
  if (any(grepl("AC Att", x$Location))){
# extract from Game_Actions data frame only rows referring to AC and Pass and save as separate data frame #
pe_ace <- filter(x, Location == "AC Att" & Game.Action == "Pass")

# create table with Teams as rows and Movement Effects as columns #
pe_ace <- table(pe_ace$Team, pe_ace$Effect)

# transform to percentages (% pass movement effects used in AC) #
pe_ace <- round((prop.table(pe_ace) * 100), digits = 2)

# convert to data frame #
pe_ace <- as.data.frame.matrix(pe_ace)

# list of movement effects #
e <- c("Maintain", "Gain", "Penetrating", "Turnover")

# check to see if all data frames contain all columns, if not add column=0 #
pe_ace[e[!(e %in% colnames(pe_ace))]] = 0

# rename all columns with prefix P_AC_E #
colnames(pe_ace) <- paste("P_AC_E", colnames(pe_ace), sep = "_")

return(pe_ace)
  } else {(pe_ace <- setNames(as.data.frame(matrix(c(0, 0, 0, 0), ncol = 4, nrow = 1)), c("P_AC_E_Main", "P_AC_E_Gain",  "P_AC_E_Penetrating", "P_AC_E_Turnover")))
    
  return(pe_ace)}
}


pe_ace_list <- list()
for (i in seq_along(game_actions_e_list)){
  pe_ace_list[[i]] <- pe_ace_f(game_actions_e_list[[i]])
}
Create table to identify movement effects when dribbling in A25 in established attacks and convert to percentages for comparison
# A25 Movement Effect Dribble Established #
de_a25e_f <- function(x){
  if (any(grepl("A25 Att", x$Location))){
# extract from Game_Actions data frame only rows referring to A25 and Dribble and save as separate data frame #
de_a25e <- filter(x, Location == "A25 Att" & Game.Action == "Dribble")

# create table with Teams as rows and Movement Effects as columns #
de_a25e <- table(de_a25e$Team, de_a25e$Effect)

# transform to percentages (% dribble movement effects used in A25) #
de_a25e <- round((prop.table(de_a25e) * 100), digits = 2)

# convert to data frame #
de_a25e <- as.data.frame.matrix(de_a25e)

# list of movement effects #
e <- c("Maintain", "Gain", "Penetrating", "Turnover")

# check to see if all data frames contain all columns, if not add column=0 #
de_a25e[e[!(e %in% colnames(de_a25e))]] = 0

# rename all columns with prefix D_A25_E #
colnames(de_a25e) <- paste("D_A25_E", colnames(de_a25e), sep = "_")

return(de_a25e)
  } else {(de_a25e <- setNames(as.data.frame(matrix(c(0, 0, 0, 0), ncol = 4, nrow = 1)), c("D_A25_E_Main", "D_A25_E_Gain",  "D_A25_E_Penetrating", "D_A25_E_Turnover")))
    
  return(de_a25e)}
}


de_a25e_list <- list()
for (i in seq_along(game_actions_e_list)){
  de_a25e_list[[i]] <- de_a25e_f(game_actions_e_list[[i]])
}
Create table to identify movement effects when passing in A25 in established attacks and convert to percentages for comparison
# A25 Movement Effect Pass Established #
pe_a25e_f <- function(x){
  if (any(grepl("A25 Att", x$Location))){
# extract from Game_Actions data frame only rows referring to A25 and Pass and save as separate data frame #
pe_a25e <- filter(x, Location == "A25 Att" & Game.Action == "Pass")

# create table with Teams as rows and Movement Effects as columns #
pe_a25e <- table(pe_a25e$Team, pe_a25e$Effect)

# transform to percentages (% pass movement effects used in A25) #
pe_a25e <- round((prop.table(pe_a25e) * 100), digits = 2)

# convert to data frame #
pe_a25e <- as.data.frame.matrix(pe_a25e)

# list of movement effects #
e <- c("Maintain", "Gain", "Penetrating", "Turnover")

# check to see if all data frames contain all columns, if not add column=0 #
pe_a25e[e[!(e %in% colnames(pe_a25e))]] = 0

# rename all columns with prefix P_A25_E #
colnames(pe_a25e) <- paste("P_A25_E", colnames(pe_a25e), sep = "_")

return(pe_a25e)
  } else {(pe_a25e <- setNames(as.data.frame(matrix(c(0, 0, 0, 0), ncol = 4, nrow = 1)), c("P_A25_E_Main", "P_A25_E_Gain",  "P_A25_E_Penetrating", "P_A25_E_Turnover")))
    
  return(pe_a25e)}
}


pe_a25e_list <- list()
for (i in seq_along(game_actions_e_list)){
  pe_a25e_list[[i]] <- pe_a25e_f(game_actions_e_list[[i]])
}
Create table to identify movement effects when dribbling in A50 in established attacks and convert to percentages for comparison
# A50 Movement Effect Dribble Established #
de_a50e_f <- function(x){
  if (any(grepl("A50 Att", x$Location))){
# extract from Game_Actions data frame only rows referring to A50 and Dribble and save as separate data frame #
de_a50e <- filter(x, Location == "A50 Att" & Game.Action == "Dribble")

# create table with Teams as rows and Movement Effects as columns #
de_a50e <- table(de_a50e$Team, de_a50e$Effect)

# transform to percentages (% dribble movement effects used in A50) #
de_a50e <- round((prop.table(de_a50e) * 100), digits = 2)

# convert to data frame #
de_a50e <- as.data.frame.matrix(de_a50e)

# list of movement effects #
e <- c("Maintain", "Gain", "Penetrating", "Turnover")

# check to see if all data frames contain all columns, if not add column=0 #
de_a50e[e[!(e %in% colnames(de_a50e))]] = 0

# rename all columns with prefix D_A50_E #
colnames(de_a50e) <- paste("D_A50_E", colnames(de_a50e), sep = "_")

return(de_a50e)
  } else {(de_a50e <- setNames(as.data.frame(matrix(c(0, 0, 0, 0), ncol = 4, nrow = 1)), c("D_A50_E_Main", "D_A50_E_Gain",  "D_A50_E_Penetrating", "D_A50_E_Turnover")))
    
  return(de_a50e)}
}


de_a50e_list <- list()
for (i in seq_along(game_actions_e_list)){
  de_a50e_list[[i]] <- de_a50e_f(game_actions_e_list[[i]])
}
Create table to identify movement effects when passing in A50 in established attacks and convert to percentages for comparison
# A50 Movement Effect Pass Established #
pe_a50e_f <- function(x){
  if (any(grepl("A50 Att", x$Location))){
# extract from Game_Actions data frame only rows referring to A50 and Pass and save as separate data frame #
pe_a50e <- filter(x, Location == "A50 Att" & Game.Action == "Pass")

# create table with Teams as rows and Movement Effects as columns #
pe_a50e <- table(pe_a50e$Team, pe_a50e$Effect)

# transform to percentages (% pass movement effects used in A50) #
pe_a50e <- round((prop.table(pe_a50e) * 100), digits = 2)

# convert to data frame #
pe_a50e <- as.data.frame.matrix(pe_a50e)

# list of movement effects #
e <- c("Maintain", "Gain", "Penetrating", "Turnover")

# check to see if all data frames contain all columns, if not add column=0 #
pe_a50e[e[!(e %in% colnames(pe_a50e))]] = 0

# rename all columns with prefix P_A50_E #
colnames(pe_a50e) <- paste("P_A50_E", colnames(pe_a50e), sep = "_")

return(pe_a50e)
  } else {(pe_a50e <- setNames(as.data.frame(matrix(c(0, 0, 0, 0), ncol = 4, nrow = 1)), c("P_A50_E_Main", "P_A50_E_Gain",  "P_A50_E_Penetrating", "P_A50_E_Turnover")))
    
  return(pe_a50e)}
}


pe_a50e_list <- list()
for (i in seq_along(game_actions_e_list)){
  pe_a50e_list[[i]] <- pe_a50e_f(game_actions_e_list[[i]])
}
Create table to identify movement effects when dribbling in D50 in established attacks and convert to percentages for comparison
# D50 Movement Effect Dribble Established #
de_d50e_f <- function(x){
  if (any(grepl("D50 Att", x$Location))){
# extract from Game_Actions data frame only rows referring to D50 and Dribble and save as separate data frame #
de_d50e <- filter(x, Location == "D50 Att" & Game.Action == "Dribble")

# create table with Teams as rows and Movement Effects as columns #
de_d50e <- table(de_d50e$Team, de_d50e$Effect)

# transform to percentages (% dribble movement effects used in D50) #
de_d50e <- round((prop.table(de_d50e) * 100), digits = 2)

# convert to data frame #
de_d50e <- as.data.frame.matrix(de_d50e)

# list of movement effects #
e <- c("Maintain", "Gain", "Penetrating", "Turnover")

# check to see if all data frames contain all columns, if not add column=0 #
de_d50e[e[!(e %in% colnames(de_d50e))]] = 0

# rename all columns with prefix D_D50_E #
colnames(de_d50e) <- paste("D_D50_E", colnames(de_d50e), sep = "_")

return(de_d50e)
  } else {(de_d50e <- setNames(as.data.frame(matrix(c(0, 0, 0, 0), ncol = 4, nrow = 1)), c("D_D50_E_Main", "D_D50_E_Gain",  "D_D50_E_Penetrating", "D_D50_E_Turnover")))
    
  return(de_d50e)}
}


de_d50e_list <- list()
for (i in seq_along(game_actions_e_list)){
  de_d50e_list[[i]] <- de_d50e_f(game_actions_e_list[[i]])
}
Create table to identify movement effects when passing in D50 in established attacks and convert to percentages for comparison
# D50 Movement Effect Pass Established #
pe_d50e_f <- function(x){
  if (any(grepl("D50 Att", x$Location))){
# extract from Game_Actions data frame only rows referring to D50 and Pass and save as separate data frame #
pe_d50e <- filter(x, Location == "D50 Att" & Game.Action == "Pass")

# create table with Teams as rows and Movement Effects as columns #
pe_d50e <- table(pe_d50e$Team, pe_d50e$Effect)

# transform to percentages (% pass movement effects used in D50) #
pe_d50e <- round((prop.table(pe_d50e) * 100), digits = 2)

# convert to data frame #
pe_d50e <- as.data.frame.matrix(pe_d50e)

# list of movement effects #
e <- c("Maintain", "Gain", "Penetrating", "Turnover")

# check to see if all data frames contain all columns, if not add column=0 #
pe_d50e[e[!(e %in% colnames(pe_d50e))]] = 0

# rename all columns with prefix P_D50_E #
colnames(pe_d50e) <- paste("P_D50_E", colnames(pe_d50e), sep = "_")

return(pe_d50e)
  } else {(pe_d50e <- setNames(as.data.frame(matrix(c(0, 0, 0, 0), ncol = 4, nrow = 1)), c("P_D50_E_Main", "P_D50_E_Gain",  "P_D50_E_Penetrating", "P_D50_E_Turnover")))
    
  return(pe_d50e)}
}


pe_d50e_list <- list()
for (i in seq_along(game_actions_e_list)){
  pe_d50e_list[[i]] <- pe_d50e_f(game_actions_e_list[[i]])
}
Create table to identify movement effects when dribbling in D25 in established attacks and convert to percentages for comparison
# D25 Movement Effect Dribble Established #
de_d25e_f <- function(x){
  if (any(grepl("D25 Att", x$Location))){
# extract from Game_Actions data frame only rows referring to D25 and Dribble and save #
# as separate data frame #
de_d25e <- filter(x, Location == "D25 Att" & Game.Action == "Dribble")

# create table with Teams as rows and Movement Effects as columns #
de_d25e <- table(de_d25e$Team, de_d25e$Effect)

# transform to percentages (% dribble movement effects used in D25) #
de_d25e <- round((prop.table(de_d25e) * 100), digits = 2)

# convert to data frame #
de_d25e <- as.data.frame.matrix(de_d25e)

# list of movement effects #
e <- c("Maintain", "Gain", "Penetrating", "Turnover")

# check to see if all data frames contain all columns, if not add column=0 #
de_d25e[e[!(e %in% colnames(de_d25e))]] = 0

# rename all columns with prefix D_D25_E #
colnames(de_d25e) <- paste("D_D25_E", colnames(de_d25e), sep = "_")

return(de_d25e)
  } else {(de_d25e <- setNames(as.data.frame(matrix(c(0, 0, 0, 0), ncol = 4, nrow = 1)), c("D_D25_E_Main", "D_D25_E_Gain",  "D_D25_E_Penetrating", "D_D25_E_Turnover")))
    
  return(de_d25e)}
}


de_d25e_list <- list()
for (i in seq_along(game_actions_e_list)){
  de_d25e_list[[i]] <- de_d25e_f(game_actions_e_list[[i]])
}
Create table to identify movement effects when passing in D25 in established attacks and convert to percentages for comparison
# D25 Movement Effect Pass Established #
pe_d25e_f <- function(x){
  if (any(grepl("D25 Att", x$Location))){
# extract from Game_Actions data frame only rows referring to D25 and Pass and save as separate data frame # 
pe_d25e <- filter(x, Location == "D25 Att" & Game.Action == "Pass")

# create table with Teams as rows and Movement Effects as columns #
pe_d25e <- table(pe_d25e$Team, pe_d25e$Effect)

# transform to percentages (% pass movement effects used in D25) #
pe_d25e <- round((prop.table(pe_d25e) * 100), digits = 2)

# convert to data frame #
pe_d25e <- as.data.frame.matrix(pe_d25e)

# list of movement effects #
e <- c("Maintain", "Gain", "Penetrating", "Turnover")

# check to see if all data frames contain all columns, if not add column=0 #
pe_d25e[e[!(e %in% colnames(pe_d25e))]] = 0

# rename all columns with prefix P_D25_E #
colnames(pe_d25e) <- paste("P_D25_E", colnames(pe_d25e), sep = "_")

return(pe_d25e)
  } else {(pe_d25e <- setNames(as.data.frame(matrix(c(0, 0, 0, 0), ncol = 4, nrow = 1)), c("P_D25_E_Main", "P_D25_E_Gain",  "P_D25_E_Penetrating", "P_D25_E_Turnover")))
    
  return(pe_d25e)}
}


pe_d25e_list <- list()
for (i in seq_along(game_actions_e_list)){
  pe_d25e_list[[i]] <- pe_d25e_f(game_actions_e_list[[i]])
}
Create table to identify movement effects when dribbling in AC in counter attacks and convert to percentages for comparison
# AC Movement Effect Dribble Counter #
de_acc_f <- function(x){
  if (any(grepl("AC Att", x$Location))){
# extract from Game_Actions data frame only rows referring to AC and Dribble and save as separate data frame #
de_acc <- filter(x, Location == "AC Att" & Game.Action == "Dribble")

# create table with Teams as rows and Movement Effects as columns #
de_acc <- table(de_acc$Team, de_acc$Effect)

# transform to percentages (% dribble movement effects used in AC) #
de_acc <- round((prop.table(de_acc) * 100), digits = 2)

# convert to data frame #
de_acc <- as.data.frame.matrix(de_acc)

# list of movement effects #
e <- c("Maintain", "Gain", "Penetrating", "Turnover")

# check to see if all data frames contain all columns, if not add column=0 #
de_acc[e[!(e %in% colnames(de_acc))]] = 0

# rename all columns with prefix D_AC_C #
colnames(de_acc) <- paste("D_AC_C", colnames(de_acc), sep = "_")

return(de_acc)
  } else {(de_acc <- setNames(as.data.frame(matrix(c(0, 0, 0, 0), ncol = 4, nrow = 1)), c("D_AC_C_Main", "D_AC_C_Gain",  "D_AC_C_Penetrating", "D_AC_C_Turnover")))
    
  return(de_acc)}
}


de_acc_list <- list()
for (i in seq_along(game_actions_c_list)){
  de_acc_list[[i]] <- de_acc_f(game_actions_c_list[[i]])
}
Create table to identify movement effects when passing in AC in counter attacks and convert to percentages for comparison
# AC Movement Effect Pass Counter #
pe_acc_f <- function(x){
  if (any(grepl("AC Att", x$Location))){
# extract from Game_Actions data frame only rows referring to AC and Pass and save as separate data frame #
pe_acc <- filter(x, Location == "AC Att" & Game.Action == "Pass")

# create table with Teams as rows and Movement Effects as columns #
pe_acc <- table(pe_acc$Team, pe_acc$Effect)

# transform to percentages (% pass movement effects used in AC) #
pe_acc <- round((prop.table(pe_acc) * 100), digits = 2)

# convert to data frame #
pe_acc <- as.data.frame.matrix(pe_acc)

# list of movement effects #
e <- c("Maintain", "Gain", "Penetrating", "Turnover")

# check to see if all data frames contain all columns, if not add column=0 #
pe_acc[e[!(e %in% colnames(pe_acc))]] = 0

# rename all columns with prefix P_AC_C #
colnames(pe_acc) <- paste("P_AC_C", colnames(pe_acc), sep = "_")

return(pe_acc)
  } else {(pe_acc <- setNames(as.data.frame(matrix(c(0, 0, 0, 0), ncol = 4, nrow = 1)), c("P_AC_C_Main", "P_AC_C_Gain",  "P_AC_C_Penetrating", "P_AC_C_Turnover")))
    
  return(pe_acc)}
}


pe_acc_list <- list()
for (i in seq_along(game_actions_c_list)){
  pe_acc_list[[i]] <- pe_acc_f(game_actions_c_list[[i]])
}
Create table to identify movement effects when dribbling in A25 in counter attacks and convert to percentages for comparison
# A25 Movement Effect Dribble Counter #
de_a25c_f <- function(x){
  if (any(grepl("A25 Att", x$Location))){
# extract from Game_Actions data frame only rows referring to A25 and Dribble and save as separate data frame #
de_a25c <- filter(x, Location == "A25 Att" & Game.Action == "Dribble")

# create table with Teams as rows and Movement Effects as columns #
de_a25c <- table(de_a25c$Team, de_a25c$Effect)

# transform to percentages (% dribble movement effects used in A25) #
de_a25c <- round((prop.table(de_a25c) * 100), digits = 2)

# convert to data frame #
de_a25c <- as.data.frame.matrix(de_a25c)

# list of movement effects #
e <- c("Maintain", "Gain", "Penetrating", "Turnover")

# check to see if all data frames contain all columns, if not add column=0 #
de_a25c[e[!(e %in% colnames(de_a25c))]] = 0

# rename all columns with prefix D_A25_C #
colnames(de_a25c) <- paste("D_A25_C", colnames(de_a25c), sep = "_")

return(de_a25c)
  } else {(de_a25c <- setNames(as.data.frame(matrix(c(0, 0, 0, 0), ncol = 4, nrow = 1)), c("D_A25_C_Main", "D_A25_C_Gain",  "D_A25_C_Penetrating", "D_A25_C_Turnover")))
    
  return(de_a25c)}
}


de_a25c_list <- list()
for (i in seq_along(game_actions_c_list)){
  de_a25c_list[[i]] <- de_a25c_f(game_actions_c_list[[i]])
}
Create table to identify movement effects when passing in A25 in counter attacks and convert to percentages for comparison
# A25 Movement Effect Pass Counter #
pe_a25c_f <- function(x){
  if (any(grepl("A25 Att", x$Location))){
# extract from Game_Actions data frame only rows referring to A25 and Pass and save as separate data frame #
pe_a25c <- filter(x, Location == "A25 Att" & Game.Action == "Pass")

# create table with Teams as rows and Movement Effects as columns #
pe_a25c <- table(pe_a25c$Team, pe_a25c$Effect)

# transform to percentages (% pass movement effects used in A25) #
pe_a25c <- round((prop.table(pe_a25c) * 100), digits = 2)

# convert to data frame #
pe_a25c <- as.data.frame.matrix(pe_a25c)

# list of movement effects #
e <- c("Maintain", "Gain", "Penetrating", "Turnover")

# check to see if all data frames contain all columns, if not add column=0 #
pe_a25c[e[!(e %in% colnames(pe_a25c))]] = 0

# rename all columns with prefix P_A25_C #
colnames(pe_a25c) <- paste("P_A25_C", colnames(pe_a25c), sep = "_")

return(pe_a25c)
  } else {(pe_a25c <- setNames(as.data.frame(matrix(c(0, 0, 0, 0), ncol = 4, nrow = 1)), c("P_A25_C_Main", "P_A25_C_Gain",  "P_A25_C_Penetrating", "P_A25_C_Turnover")))
    
  return(pe_a25c)}
}


pe_a25c_list <- list()
for (i in seq_along(game_actions_c_list)){
  pe_a25c_list[[i]] <- pe_a25c_f(game_actions_c_list[[i]])
}
Create table to identify movement effects when dribbling in A50 in counter attacks and convert to percentages for comparison
# A50 Movement Effect Dribble Counter #
de_a50c_f <- function(x){
  if (any(grepl("A50 Att", x$Location))){
# extract from Game_Actions data frame only rows referring to A50 and Dribble save as separate data frame #
de_a50c <- filter(x, Location == "A50 Att" & Game.Action == "Dribble")

# create table with Teams as rows and Movement Effects as columns #
de_a50c <- table(de_a50c$Team, de_a50c$Effect)

# transform to percentages (% dribble movement effects used in A50) #
de_a50c <- round((prop.table(de_a50c) * 100), digits = 2)

# convert to data frame #
de_a50c <- as.data.frame.matrix(de_a50c)

# list of movement effects #
e <- c("Maintain", "Gain", "Penetrating", "Turnover")

# check to see if all data frames contain all columns, if not add column=0 #
de_a50c[e[!(e %in% colnames(de_a50c))]] = 0

# rename all columns with prefix D_A50_C #
colnames(de_a50c) <- paste("D_A50_C", colnames(de_a50c), sep = "_")

return(de_a50c)
  } else {(de_a50c <- setNames(as.data.frame(matrix(c(0, 0, 0, 0), ncol = 4, nrow = 1)), c("D_A50_C_Main", "D_A50_C_Gain",  "D_A50_C_Penetrating", "D_A50_C_Turnover")))
    
  return(de_a50c)}
}


de_a50c_list <- list()
for (i in seq_along(game_actions_c_list)){
  de_a50c_list[[i]] <- de_a50c_f(game_actions_c_list[[i]])
}
Create table to identify movement effects when passing in A50 in counter attacks and convert to percentages for comparison
# A50 Movement Effect Pass Counter #
pe_a50c_f <- function(x){
  if (any(grepl("A50 Att", x$Location))){
# extract from Game_Actions data frame only rows referring to A50 and Pass and save as separate data frame #
pe_a50c <- filter(x, Location == "A50 Att" & Game.Action == "Pass")

# create table with Teams as rows and Movement Effects as columns #
pe_a50c <- table(pe_a50c$Team, pe_a50c$Effect)

# transform to percentages (% pass movement effects used in A50 )#
pe_a50c <- round((prop.table(pe_a50c) * 100), digits = 2)

# convert to data frame #
pe_a50c <- as.data.frame.matrix(pe_a50c)

# list of movement effects #
e <- c("Maintain", "Gain", "Penetrating", "Turnover")

# check to see if all data frames contain all columns, if not add column=0 #
pe_a50c[e[!(e %in% colnames(pe_a50c))]] = 0

# rename all columns with prefix P_A50_C #
colnames(pe_a50c) <- paste("P_A50_C", colnames(pe_a50c), sep = "_")

return(pe_a50c)
  } else {(pe_a50c <- setNames(as.data.frame(matrix(c(0, 0, 0, 0), ncol = 4, nrow = 1)), c("P_A50_C_Main", "P_A50_C_Gain",  "P_A50_C_Penetrating", "P_A50_C_Turnover")))
    
  return(pe_a50c)}
}


pe_a50c_list <- list()
for (i in seq_along(game_actions_c_list)){
  pe_a50c_list[[i]] <- pe_a50c_f(game_actions_c_list[[i]])
}
Create table to identify movement effects when dribbling in D50 in counter attacks and convert to percentages for comparison
# D50 Movement Effect Dribble Counter #
de_d50c_f <- function(x){
  if (any(grepl("D50 Att", x$Location))){
# extract from Game_Actions data frame only rows referring to D50 and Dribble and save as separate data frame #
de_d50c <- filter(x, Location == "D50 Att" & Game.Action == "Dribble")

# create table with Teams as rows and Movement Effects as columns #
de_d50c <- table(de_d50c$Team, de_d50c$Effect)

# transform to percentages (% dribble movement effects used in D50) #
de_d50c <- round((prop.table(de_d50c) * 100), digits = 2)

# convert to data frame #
de_d50c <- as.data.frame.matrix(de_d50c)

# list of movement effects #
e <- c("Maintain", "Gain", "Penetrating", "Turnover")

# check to see if all data frames contain all columns, if not add column=0 #
de_d50c[e[!(e %in% colnames(de_d50c))]] = 0

# rename all columns with prefix D_D50_C #
colnames(de_d50c) <- paste("D_D50_C", colnames(de_d50c), sep = "_")

return(de_d50c)
  } else {(de_d50c <- setNames(as.data.frame(matrix(c(0, 0, 0, 0), ncol = 4, nrow = 1)), c("D_D50_C_Main", "D_D50_C_Gain",  "D_D50_C_Penetrating", "D_D50_C_Turnover")))
    
  return(de_d50c)}
}


de_d50c_list <- list()
for (i in seq_along(game_actions_c_list)){
  de_d50c_list[[i]] <- de_d50c_f(game_actions_c_list[[i]])
}
Create table to identify movement effects when passing in D50 in counter attacks and convert to percentages for comparison
# D50 Movement Effect Pass Counter #
pe_d50c_f <- function(x){
  if (any(grepl("D50 Att", x$Location))){
# extract from Game_Actions data frame only rows referring to D50 and Pass and save as separate data frame #
pe_d50c <- filter(x, Location == "D50 Att" & Game.Action == "Pass")

# create table with Teams as rows and Movement Effects as columns #
pe_d50c <- table(pe_d50c$Team, pe_d50c$Effect)

# transform to percentages (% pass movement effects used in D50) #
pe_d50c <- round((prop.table(pe_d50c) * 100), digits = 2)

# convert to data frame #
pe_d50c <- as.data.frame.matrix(pe_d50c)

# list of movement effects #
e <- c("Maintain", "Gain", "Penetrating", "Turnover")

# check to see if all data frames contain all columns, if not add column=0 #
pe_d50c[e[!(e %in% colnames(pe_d50c))]] = 0

# rename all columns with prefix P_D50_C #
colnames(pe_d50c) <- paste("P_D50_C", colnames(pe_d50c), sep = "_")

return(pe_d50c)
  } else {(pe_d50c <- setNames(as.data.frame(matrix(c(0, 0, 0, 0), ncol = 4, nrow = 1)), c("P_D50_C_Main", "P_D50_C_Gain",  "P_D50_C_Penetrating", "P_D50_C_Turnover")))
    
  return(pe_d50c)}
}


pe_d50c_list <- list()
for (i in seq_along(game_actions_c_list)){
  pe_d50c_list[[i]] <- pe_d50c_f(game_actions_c_list[[i]])
}
Create table to identify movement effects when dribbling in D25 in counter attacks and convert to percentages for comparison
# D25 Movement Effect Dribble Counter #
de_d25c_f <- function(x){
  if (any(grepl("D25 Att", x$Location))){
# extract from Game_Actions data frame only rows referring to D25 and Counter and save as separate data frame #
de_d25c <- filter(x, Location == "D25 Att" & Game.Action == "Dribble")

# create table with Teams as rows and Movement Effects as columns #
de_d25c <- table(de_d25c$Team, de_d25c$Effect)

# transform to percentages (% dribble movement effects used in D25) #
de_d25c <- round((prop.table(de_d25c) * 100), digits = 2)

# convert to data frame #
de_d25c <- as.data.frame.matrix(de_d25c)

# list of movement effects #
e <- c("Maintain", "Gain", "Penetrating", "Turnover")

# check to see if all data frames contain all columns, if not add column=0 #
de_d25c[e[!(e %in% colnames(de_d25c))]] = 0

# rename all columns with prefix D_D25_C #
colnames(de_d25c) <- paste("D_D25_C", colnames(de_d25c), sep = "_")

return(de_d25c)
  } else {(de_d25c <- setNames(as.data.frame(matrix(c(0, 0, 0, 0), ncol = 4, nrow = 1)), c("D_D25_C_Main", "D_D25_C_Gain",  "D_D25_C_Penetrating", "D_D25_C_Turnover")))
    
  return(de_d25c)}
}


de_d25c_list <- list()
for (i in seq_along(game_actions_c_list)){
  de_d25c_list[[i]] <- de_d25c_f(game_actions_c_list[[i]])
}
Create table to identify movement effects when passing in D25 in counter attacks and convert to percentages for comparison
# D25 Movement Effect Pass Counter #
pe_d25c_f <- function(x){
  if (any(grepl("D25 Att", x$Location))){
# extract from Game_Actions data frame only rows referring to D25 and Pass and save as separate data frame #
pe_d25c <- filter(x, Location == "D25 Att" & Game.Action == "Pass")

# create table with Teams as rows and Movement Effects as columns #
pe_d25c <- table(pe_d25c$Team, pe_d25c$Effect)

# transform to percentages (% pass movement effects used in D25) #
pe_d25c <- round((prop.table(pe_d25c) * 100), digits = 2)

# convert to data frame #
pe_d25c <- as.data.frame.matrix(pe_d25c)

# list of movement effects #
e <- c("Maintain", "Gain", "Penetrating", "Turnover")

# check to see if all data frames contain all columns, if not add column=0 #
pe_d25c[e[!(e %in% colnames(pe_d25c))]] = 0

# rename all columns with prefix P_D25_C #
colnames(pe_d25c) <- paste("P_D25_C", colnames(pe_d25c), sep = "_")

return(pe_d25c)
  } else {(pe_d25c <- setNames(as.data.frame(matrix(c(0, 0, 0, 0), ncol = 4, nrow = 1)), c("P_D25_C_Main", "P_D25_C_Gain",  "P_D25_C_Penetrating", "P_D25_C_Turnover")))
    
  return(pe_d25c)}
}


pe_d25c_list <- list()
for (i in seq_along(game_actions_c_list)){
  pe_d25c_list[[i]] <- pe_d25c_f(game_actions_c_list[[i]])
}
Join all teams dribbling and passing movement effect data frames per location into one
# Movement Effects per location per team #
me_list <- list()
for (i in seq_along(game_list)){
  me_list[[i]] <- bind_cols(de_ace_list[[i]], de_a25e_list[[i]], de_a50e_list[[i]], de_d50e_list[[i]], de_d25e_list[[i]], pe_ace_list[[i]], pe_a25e_list[[i]], pe_a50e_list[[i]], pe_d50e_list[[i]], pe_d25e_list[[i]], de_acc_list[[i]], de_a25c_list[[i]], de_a50c_list[[i]], de_d50c_list[[i]], de_d25c_list[[i]], pe_acc_list[[i]], pe_a25c_list[[i]], pe_a50c_list[[i]], pe_d50c_list[[i]], pe_d25c_list[[i]])
}

me_list <- bind_rows(me_list)

5. EXPORT DATA

Calculate the league average for all teams movement effects and tidy table for presentation
# add team names #
me_list <- rownames_to_column(me_list, "Team")

# calculate average per variable #
me_avg <- data.frame(Percent = colMeans(me_list[, c(-1)]))

# add variable names #
me_avg <- rownames_to_column(me_avg, "Variable")
me_avg <- separate(me_avg, Variable, c("Action", "Location", "Attack", "Effect"))

# remove penetrating effect column #
me_avg <- filter(me_avg, Effect != "Penetrating")

# change to factors and reorder #
me_avg$Action <- factor(me_avg$Action, levels = c("D", "P"), labels = c("Dribble", "Pass"))
me_avg$Effect <- factor(me_avg$Effect, levels = c("Maintain", "Gain", "Turnover"))
me_avg$Location <- factor(me_avg$Location, levels = c("AC", "A25", "A50", "D50", "D25"))

# save data frame #
write_xlsx(me_avg, "Data/Processed Data//Game_Actions_Avg_Women.xlsx")

me_avg2 <- head(me_avg)
knitr::kable(me_avg2, caption = "League Movement Effect Averages")
League Movement Effect Averages
Action Location Attack Effect Percent
Dribble AC E Gain 13.27444
Dribble AC E Maintain 54.39444
Dribble AC E Turnover 31.96556
Dribble A25 E Gain 18.97778
Dribble A25 E Maintain 63.60667
Dribble A25 E Turnover 17.32333
Calculate team averages for movement effects and tidy table for presentation
# transform from wide to long #
me_list_long <- me_list %>% 
  pivot_longer(cols = D_AC_E_Gain:P_D25_C_Penetrating,
               names_to = "Variable",
               values_to = "Percent")

# separate descriptive column #
me_list_long <- separate(me_list_long, Variable, c("Action", "Location", "Attack", "Effect"))

# remove penetrating effect column #
me_list_long <- filter(me_list_long, Effect != "Penetrating")

# change to factors and reorder #
me_list_long$Action <- factor(me_list_long$Action, levels = c("D", "P"), labels = c("Dribble", "Pass"))
me_list_long$Effect <- factor(me_list_long$Effect, levels = c("Maintain", "Gain", "Turnover"))
me_list_long$Location <- factor(me_list_long$Location, levels = c("AC", "A25", "A50", "D50", "D25"))

# save data frame #
write_xlsx(me_list_long, "Data/Processed Data//Game_Actions_Women.xlsx")

me_list_long2 <- head(me_list_long)
knitr::kable(me_list_long2, caption = "Team Movement Effect Averages")
Team Movement Effect Averages
Team Action Location Attack Effect Percent
Argentina Dribble AC E Gain 9.72
Argentina Dribble AC E Maintain 59.03
Argentina Dribble AC E Turnover 31.25
Argentina Dribble A25 E Gain 21.10
Argentina Dribble A25 E Maintain 62.29
Argentina Dribble A25 E Turnover 16.45

STOPPAGES

6. FILTER

Extract stoppage rows, select and rename descriptive columns
stoppages_f <- function(x){
# extract from all data (Game) rows referring to Stoppages and save as separate data frame (Stoppages) #
stoppages <- filter(x, A == "Stoppages")

# rename columns #
stoppages <- plyr::rename(stoppages, c("descriptors..." = "Stop.Type", 
                                       "...7" = "Restart.Speed", 
                                       "...8" = "Match.Status", 
                                       "...9" = "Match.Location", 
                                       "...10" = "Quality.Opp", 
                                       "...11" = "Location", 
                                       "...12" = "Attack.Type"))

# remove unnecessary columns #
stoppages <- select(stoppages, Match_Team, Team, Opposition, Gender, Stop.Type, Restart.Speed,  Match.Status, Match.Location, Quality.Opp, Location, Attack.Type)

return(stoppages)
}

stoppages_list <- list()
for (i in seq_along(game_list)){
  stoppages_list[[i]] <- stoppages_f(game_list[[i]])
}
Extract stoppages performed during established attacks
# filter for attack type #
stoppages_e_list <- list()
for (i in seq_along(stoppages_list)){
  stoppages_e_list[[i]] <- filter(stoppages_list[[i]], Attack.Type == "Established")
}
Extract stoppages performed during counter attacks
# filter for attack type #
stoppages_c_list <- list()
for (i in seq_along(stoppages_list)){
  stoppages_c_list[[i]] <- filter(stoppages_list[[i]],  Attack.Type == "Counter")
}

7. STOPPAGE TYPES PER ATTACK TYPE PER LOCATION

Create table to identify stoppage types in AC in established attacks and convert to percentages for comparison
# AC Stoppages Established #
s_ace_f <- function(x){
    if (any(grepl("AC Att", x$Location))){
# extract from Stoppages data frame only rows referring to AC and save as separate data frame #
s_ace <- filter(x, Location == "AC Att")

# create table with Teams as rows and Stoppage Types as columns #
s_ace <- table(s_ace$Team, s_ace$Stop.Type)

# transform to percentages (% stoppage types used in AC) #
s_ace <- round((prop.table(s_ace) * 100), digits = 2)

# convert to data frame #
s_ace <- as.data.frame.matrix(s_ace)

# list of stoppages #
g <- c("Long Corner", "Penalty Corner", "Penalty Stroke")

# check to see if all data frames contain all columns, if not add column=0 #
s_ace[g[!(g %in% colnames(s_ace))]] = 0

# rename columns #
s_ace <- plyr::rename(s_ace, c("Penalty Corner" = "PC", 
                               "Long Corner" = "LC",
                               "Penalty Stroke" = "PS"))
                      
# rename all columns with prefix AC_E #
colnames(s_ace) <- paste("AC_E", colnames(s_ace), sep = "_")

return(s_ace)
    } else {(s_ace <- setNames(as.data.frame(matrix(c(0, 0, 0), ncol = 3, nrow = 1)), c("AC_E_PC", "AC_E_LC", "AC_E_PS")))
      
  return(s_ace)}
}

s_ace_list <- list()
for (i in seq_along(stoppages_e_list)){
  s_ace_list[[i]] <- s_ace_f(stoppages_e_list[[i]])
}
Create table to identify stoppage types in A25 in established attacks and convert to percentages for comparison
# A25 Stoppages Established #
s_a25e_f <- function(x){
    if (any(grepl("A25 Att", x$Location))){
# extract from Stoppages data frame only rows referring to A25 and save as separate data frame #
s_a25e <- filter(x, Location == "A25 Att")

# create table with Teams as rows and Stoppage Types as columns #
s_a25e <- table(s_a25e$Team, s_a25e$Stop.Type)

# transform to percentages (% stoppage types used in A25) #
s_a25e <- round((prop.table(s_a25e) * 100), digits = 2)

# convert to data frame #
s_a25e <- as.data.frame.matrix(s_a25e)

# list of stoppages #
g <- c("Long Corner", "Penalty Corner", "Sideline", "Free Hit")

# check to see if all data frames contain all columns, if not add column=0 #
s_a25e[g[!(g %in% colnames(s_a25e))]] = 0

# rename columns #
s_a25e <- plyr::rename(s_a25e, c("Penalty Corner" = "PC", 
                                 "Long Corner" = "LC", 
                                 "Sideline" = "S", 
                                 "Free Hit" = "FH"))
                      
# rename all columns with prefix A25_E #
colnames(s_a25e) <- paste("A25_E", colnames(s_a25e), sep = "_")

return(s_a25e)
    } else {(s_a25e <- setNames(as.data.frame(matrix(c(0, 0, 0, 0), ncol = 4, nrow = 1)), c("A25_E_PC", "A25_E_LC", "A25_E_S", "A25_E_FH")))
      
  return(s_a25e)}
}

s_a25e_list <- list()
for (i in seq_along(stoppages_e_list)){
  s_a25e_list[[i]] <- s_a25e_f(stoppages_e_list[[i]])
}
Create table to identify stoppage types in A50 in established attacks and convert to percentages for comparison
# A50 Stoppages Established #
s_a50e_f <- function(x){
    if (any(grepl("A50 Att", x$Location))){
# extract from Stoppages data frame only rows referring to A50 and save as separate data frame #
s_a50e <- filter(x, Location == "A50 Att")

# create table with Teams as rows and Stoppage Types as columns #
s_a50e <- table(s_a50e$Team, s_a50e$Stop.Type)

# transform to percentages (% stoppage types used in A50) #
s_a50e <- round((prop.table(s_a50e) * 100), digits = 2)

# convert to data frame #
s_a50e <- as.data.frame.matrix(s_a50e)

# list of stoppages #
g <- c("Free Hit", "Sideline")

# check to see if all data frames contain all columns, if not add column=0 #
s_a50e[g[!(g %in% colnames(s_a50e))]] = 0

# rename columns #
s_a50e <- plyr::rename(s_a50e, c( "Sideline" = "S", 
                                  "Free Hit" = "FH"))
                      
# rename all columns with prefix A50_E #
colnames(s_a50e) <- paste("A50_E", colnames(s_a50e), sep = "_")

return(s_a50e)
    } else {(s_a50e <- setNames(as.data.frame(matrix(c(0, 0), ncol = 2, nrow = 1)), c("A50_E_S", "A50_E_FH")))
      
  return(s_a50e)}
}

s_a50e_list <- list()
for (i in seq_along(stoppages_e_list)){
  s_a50e_list[[i]] <- s_a50e_f(stoppages_e_list[[i]])
}
Create table to identify stoppage types in D50 in established attacks and convert to percentages for comparison
# D50 Stoppages Established #
s_d50e_f <- function(x){
    if (any(grepl("D50 Att", x$Location))){
# extract from Stoppages data frame only rows referring to D50 and save as separate data frame #
s_d50e <- filter(x, Location == "D50 Att")

# create table with Teams as rows and Stoppage Types as columns #
s_d50e <- table(s_d50e$Team, s_d50e$Stop.Type)

# transform to percentages (% stoppage types used in D50) #
s_d50e <- round((prop.table(s_d50e) * 100), digits = 2)

# convert to data frame #
s_d50e <- as.data.frame.matrix(s_d50e)

# list of stoppages #
g <- c("Free Hit", "Sideline")

# check to see if all data frames contain all columns, if not add column=0 #
s_d50e[g[!(g %in% colnames(s_d50e))]] = 0

# rename columns #
s_d50e <- plyr::rename(s_d50e, c( "Sideline" = "S", 
                                  "Free Hit" = "FH"))
                      
# rename all columns with prefix D50_E #
colnames(s_d50e) <- paste("D50_E", colnames(s_d50e), sep = "_")

return(s_d50e)
    } else {(s_d50e <- setNames(as.data.frame(matrix(c(0, 0), ncol = 2, nrow = 1)), c("D50_E_S", "D50_E_FH")))
      
  return(s_d50e)}
}

s_d50e_list <- list()
for (i in seq_along(stoppages_e_list)){
  s_d50e_list[[i]] <- s_d50e_f(stoppages_e_list[[i]])
}
Create table to identify stoppage types in D25 in established attacks and convert to percentages for comparison
# D25 Stoppages #
s_d25e_f <- function(x){
    if (any(grepl("D25 Att", x$Location))){
# extract from Stoppages data frame only rows referring to D25 and save as separate data frame #
s_d25e <- filter(x, Location == "D25 Att")

# create table with Teams as rows and Stoppage Types as columns #
s_d25e <- table(s_d25e$Team, s_d25e$Stop.Type)

# transform to percentages (% stoppage types used in D25) #
s_d25e <- round((prop.table(s_d25e) * 100), digits = 2)

# convert to data frame #
s_d25e <- as.data.frame.matrix(s_d25e)

# list of stoppages #
g <- c("Free Hit", "Sideline", "16")

# check to see if all data frames contain all columns, if not add column=0 #
s_d25e[g[!(g %in% colnames(s_d25e))]] = 0

# rename columns #
s_d25e <- plyr::rename(s_d25e, c( "Sideline" = "S", 
                                  "Free Hit" = "FH"))
                      
# rename all columns with prefix D25_E #
colnames(s_d25e) <- paste("D25_E", colnames(s_d25e), sep = "_")

return(s_d25e)
    } else {(s_d25e <- setNames(as.data.frame(matrix(c(0, 0, 0), ncol = 3, nrow = 1)), c("D25_E_S", "D25_E_FH", "D25_E_16")))
      
  return(s_d25e)}
}

s_d25e_list <- list()
for (i in seq_along(stoppages_e_list)){
  s_d25e_list[[i]] <- s_d25e_f(stoppages_e_list[[i]])
}
Create table to identify stoppage types in AC in counter attacks and convert to percentages for comparison
# AC Stoppages Counter #
s_acc_f <- function(x){
    if (any(grepl("AC Att", x$Location))){
# extract from Stoppages data frame only rows referring to AC and save as separate data frame #
s_acc <- filter(x, Location == "AC Att")

# create table with Teams as rows and Stoppage Types as columns #
s_acc <- table(s_acc$Team, s_acc$Stop.Type)

# transform to percentages (% stoppage types used in AC) #
s_acc <- round((prop.table(s_acc) * 100), digits = 2)

# convert to data frame #
s_acc <- as.data.frame.matrix(s_acc)

# list of stoppages #
g <- c("Long Corner", "Penalty Corner", "Penalty Stroke")

# check to see if all data frames contain all columns, if not add column=0 #
s_acc[g[!(g %in% colnames(s_acc))]] = 0

# rename columns #
s_acc <- plyr::rename(s_acc, c("Penalty Corner" = "PC", 
                               "Long Corner" = "LC", 
                               "Penalty Stroke" = "PS"))
                      
# rename all columns with prefix AC_C #
colnames(s_acc) <- paste("AC_C", colnames(s_acc), sep = "_")

return(s_acc)
    } else {(s_acc <- setNames(as.data.frame(matrix(c(0, 0, 0), ncol = 3, nrow = 1)), c("AC_C_PC", "AC_C_LC", "AC_C_PS")))
      
  return(s_acc)}
}

s_acc_list <- list()
for (i in seq_along(stoppages_c_list)){
  s_acc_list[[i]] <- s_acc_f(stoppages_c_list[[i]])
}
Create table to identify stoppage types in A25 in counter attacks and convert to percentages for comparison
# A25 Stoppages Counter #
s_a25c_f <- function(x){
    if (any(grepl("A25 Att", x$Location))){
# extract from Stoppages data frame only rows referring to A25 and save as separate data frame #
s_a25c <- filter(x, Location == "A25 Att")

# create table with Teams as rows and Stoppage Types as columns #
s_a25c <- table(s_a25c$Team, s_a25c$Stop.Type)

# transform to percentages (% stoppage types used in A25) #
s_a25c <- round((prop.table(s_a25c) * 100), digits = 2)

# convert to data frame #
s_a25c <- as.data.frame.matrix(s_a25c)

# list of stoppages #
g <- c("Long Corner", "Penalty Corner", "Sideline", "Free Hit")

# check to see if all data frames contain all columns, if not add column=0 #
s_a25c[g[!(g %in% colnames(s_a25c))]] = 0

# rename columns #
s_a25c <- plyr::rename(s_a25c, c("Penalty Corner" = "PC", 
                                 "Long Corner" = "LC", 
                                 "Sideline" = "S", 
                                 "Free Hit" = "FH"))
                      
# rename all columns with prefix A25_C #
colnames(s_a25c) <- paste("A25_C", colnames(s_a25c), sep = "_")

return(s_a25c)
    } else {(s_a25c <- setNames(as.data.frame(matrix(c(0, 0, 0, 0), ncol = 4, nrow = 1)), c("A25_C_PC", "A25_C_LC", "A25_C_S", "A25_C_FH")))
      
  return(s_a25c)}
}

s_a25c_list <- list()
for (i in seq_along(stoppages_c_list)){
  s_a25c_list[[i]] <- s_a25c_f(stoppages_c_list[[i]])
}
Create table to identify stoppage types in A50 in counter attacks and convert to percentages for comparison
# A50 Stoppages Counter #
s_a50c_f <- function(x){
    if (any(grepl("A50 Att", x$Location))){
# extract from Stoppages data frame only rows referring to A50 and save as separate data frame #
s_a50c <- filter(x, Location == "A50 Att")

# create table with Teams as rows and Stoppage Types as columns #
s_a50c <- table(s_a50c$Team, s_a50c$Stop.Type)

# transform to percentages (% stoppages types used in A50) #
s_a50c <- round((prop.table(s_a50c) * 100), digits = 2)

# convert to data frame #
s_a50c <- as.data.frame.matrix(s_a50c)

# list of stoppages #
g <- c("Free Hit", "Sideline")

# check to see if all data frames contain all columns, if not add column=0 #
s_a50c[g[!(g %in% colnames(s_a50c))]] = 0

# rename columns #
s_a50c <- plyr::rename(s_a50c, c( "Sideline" = "S", "Free Hit" = "FH"))
                      
# rename all columns with prefix A50_C #
colnames(s_a50c) <- paste("A50_C", colnames(s_a50c), sep = "_")

return(s_a50c)
    } else {(s_a50c <- setNames(as.data.frame(matrix(c(0, 0), ncol = 2, nrow = 1)), c("A50_C_S", "A50_C_FH")))
      
  return(s_a50c)}
}

s_a50c_list <- list()
for (i in seq_along(stoppages_c_list)){
  s_a50c_list[[i]] <- s_a50c_f(stoppages_c_list[[i]])
}
Create table to identify stoppage types in D50 in counter attacks and convert to percentages for comparison
# D50 Stoppages Counter #
s_d50c_f <- function(x){
    if (any(grepl("D50 Att", x$Location))){
# extract from Stoppages data frame only rows referring to D50 and save as separate data frame #
s_d50c <- filter(x, Location == "D50 Att")

# create table with Teams as rows and Stoppage Types as columns #
s_d50c <- table(s_d50c$Team, s_d50c$Stop.Type)

# transform to percentages (% stoppage types used in D50) #
s_d50c <- round((prop.table(s_d50c) * 100), digits = 2)

# convert to data frame #
s_d50c <- as.data.frame.matrix(s_d50c)

# list of stoppages #
g <- c("Free Hit", "Sideline")

# check to see if all data frames contain all columns, if not add column=0 #
s_d50c[g[!(g %in% colnames(s_d50c))]] = 0

# rename columns #
s_d50c <- plyr::rename(s_d50c, c( "Sideline" = "S", "Free Hit" = "FH"))
                      
# rename all columns with prefix D50_C #
colnames(s_d50c) <- paste("D50_C", colnames(s_d50c), sep = "_")

return(s_d50c)
    } else {(s_d50c <- setNames(as.data.frame(matrix(c(0, 0), ncol = 2,  nrow = 1)), c("D50_C_S", "D50_C_FH")))
      
  return(s_d50c)}
}

s_d50c_list <- list()
for (i in seq_along(stoppages_c_list)){
  s_d50c_list[[i]] <- s_d50c_f(stoppages_c_list[[i]])
}
Create table to identify stoppage types in D25 in counter attacks and convert to percentages for comparison
# D25 Stoppages Counter #
s_d25c_f <- function(x){
    if (any(grepl("D25 Att", x$Location))){
# extract from Stoppages data frame only rows referring to D25 and save as separate data frame #
s_d25c <- filter(x, Location == "D25 Att")

# create table with Teams as rows and Stoppage Types as columns #
s_d25c <- table(s_d25c$Team, s_d25c$Stop.Type)

# transform to percentages (% stoppages types used in D25) #
s_d25c <- round((prop.table(s_d25c) * 100), digits = 2)

# convert to data frame #
s_d25c <- as.data.frame.matrix(s_d25c)

# list of stoppages #
g <- c("Free Hit", "Sideline", "16")

# check to see if all data frames contain all columns, if not add column=0 #
s_d25c[g[!(g %in% colnames(s_d25c))]] = 0

# rename columns #
s_d25c <- plyr::rename(s_d25c, c( "Sideline" = "S", 
                                  "Free Hit" = "FH"))
                      
# rename all columns with prefix D25_C #
colnames(s_d25c) <- paste("D25_C", colnames(s_d25c), sep = "_")

return(s_d25c)
    } else {(s_d25c <- setNames(as.data.frame(matrix(c(0, 0, 0), ncol = 3, nrow = 1)), c("D25_C_S", "D25_C_FH", "D25_C_16")))
      
  return(s_d25c)}
}

s_d25c_list <- list()
for (i in seq_along(stoppages_c_list)){
  s_d25c_list[[i]] <- s_d25c_f(stoppages_c_list[[i]])
}
Join all teams stoppage types data frames per location into one
# Stoppages Types Total per location #
s_type_list <- list()
for (i in seq_along(game_list)){
  s_type_list[[i]] <- bind_cols(s_ace_list[[i]], s_a25e_list[[i]], s_a50e_list[[i]], s_d50e_list[[i]], s_d25e_list[[i]], s_acc_list[[i]], s_a25c_list[[i]], s_a50c_list[[i]], s_d50c_list[[i]], s_d25c_list[[i]])
}

s_type_list <- bind_rows(s_type_list)

8. EXPORT DATA

Calculate the league average for all teams stoppage types and tidy table for presentation
# add team names #
s_type_list <- rownames_to_column(s_type_list, "Team")

# calculate average per variable #
s_type_avg <- data.frame(Percent = colMeans(s_type_list[, c(-1)]))

# add variable names #
s_type_avg <- rownames_to_column(s_type_avg, "Variable")
s_type_avg <- separate(s_type_avg, Variable, c("Location", "Attack", "Stoppage"))

# remove penalty stroke column #
s_type_avg <- filter(s_type_avg, Stoppage != "PS")

# change to factor and reorder #
s_type_avg$Stoppage <- factor(s_type_avg$Stoppage, levels = c("16", "FH", "S", "LC", "PC"), labels = c("16", "Free Hit", "Sideline", "Long Corner", "Penalty Corner"))

s_type_avg$Location <- factor(s_type_avg$Location, levels = c("AC", "A25", "A50", "D50", "D25"))

#save data frame#
write_xlsx(s_type_avg, "Data/Processed Data//Stoppages_Avg_Women.xlsx")

s_type_avg2 <- head(s_type_avg)
knitr::kable(s_type_avg2, caption = "League Stoppages Type Averages")
League Stoppages Type Averages
Location Attack Stoppage Percent
AC E Long Corner 60.185556
AC E Penalty Corner 39.207778
A25 E Free Hit 50.165556
A25 E Long Corner 15.894444
A25 E Penalty Corner 1.262222
A25 E Sideline 32.675556
Calculate team averages for stoppage types and tidy table for presentation
# transform from wide to long #
s_type_list_long <- s_type_list %>% 
  pivot_longer(cols = AC_E_LC:D25_C_16,
               names_to = "Variable",
               values_to = "Percent")

# separate descriptive column #
s_type_list_long <- separate(s_type_list_long, Variable, c("Location", "Attack", "Stoppage"))

# remove penalty stroke column #
s_type_list_long <- filter(s_type_list_long, Stoppage != "PS")

# change to factor and reorder #
s_type_list_long$Stoppage <- factor(s_type_list_long$Stoppage, levels = c("16", "FH", "S", "LC", "PC"), labels = c("16", "Free Hit", "Sideline", "Long Corner", "Penalty Corner"))

s_type_list_long$Location <- factor(s_type_list_long$Location, levels = c("AC", "A25", "A50", "D50", "D25"))


#save data frame#
write_xlsx(s_type_list_long, "Data/Processed Data//Stoppages_Women.xlsx")

s_type_list_long2 <- head(s_type_list_long)
knitr::kable(s_type_list_long2, caption = "Team Stoppage Type Averages")
Team Stoppage Type Averages
Team Location Attack Stoppage Percent
Argentina AC E Long Corner 64.52
Argentina AC E Penalty Corner 35.48
Argentina A25 E Free Hit 46.85
Argentina A25 E Long Corner 17.48
Argentina A25 E Penalty Corner 0.70
Argentina A25 E Sideline 34.97

TURNOVERS

9. FILTER

Extract turnover rows, select and rename descriptive columns
turnovers_f <- function(x){
# extract from all data (Game) rows referring to Turnovers and save as separate data frame (Turnovers) #
turnovers <- filter(x, A == "Turnover")

# rename columns #
turnovers <- plyr::rename(turnovers, c("descriptors..." = "Type", 
                                       "...7" = "Def.Pressure", 
                                       "...8" = "TO.Location",
                                       "...9" = "Match.Status", 
                                       "...10" = "Match.Location", 
                                       "...11" = "Quality.Opp", 
                                       "...12" = "Location", 
                                       "...13" = "Attack.Type"))

# remove unnecessary columns #
turnovers <- select(turnovers, Match_Team, Team, Opposition, Gender, Type, Def.Pressure, TO.Location, Match.Status, Match.Location, Quality.Opp,Location, Attack.Type)

return(turnovers)
}

turnovers_list <- list()
for (i in seq_along(game_list)){
  turnovers_list[[i]] <- turnovers_f(game_list[[i]])
}
Extract turnovers performed during established attacks
# filter for attack type #
turnovers_e_list <- list()
for (i in seq_along(turnovers_list)){
  turnovers_e_list[[i]] <- filter(turnovers_list[[i]], Attack.Type == "Established")
}
Extract turnovers performed during counter attacks
# filter for attack type #
turnovers_c_list <- list()
for (i in seq_along(turnovers_list)){
  turnovers_c_list[[i]] <- filter(turnovers_list[[i]], Attack.Type == "Counter")
}

10. TURNOVER TYPES PER ATTACK TYPE PER LOCATION

Create table to identify turnover types in AC in established attacks and convert to percentages for comparison
# Turnovers Established Attack AC #
t_ace_f <- function(x){
  if (any(grepl("AC", x$TO.Location))){
# extract from Turnovers data frame only rows referring to AC and save as separate data frame #
t_ace <- filter(x, TO.Location == "AC")

# create table with Teams as rows and Turnover types as columns #
t_ace <- table(t_ace$Team, t_ace$Type)

# transform to percentages (% turnover types used in AC) #
t_ace <- round(prop.table(t_ace) * 100, digits = 2)

# convert to data frame #
t_ace <- as.data.frame.matrix(t_ace)

# list of turnover types #
m <- c("Tackle", "Intercept", "Miss Trap", "Missed Shot", "Blocked", "Lost Ball", "Out", "Free Hit", "Rebound")

# check to see if all data frames contain all columns, if not add column=0 #
t_ace[m[!(m %in% colnames(t_ace))]] = 0

# rename columns #
t_ace <- plyr::rename(t_ace, c("Tackle" = "T", 
                               "Intercept" = "I", 
                               "Miss Trap" = "MT",
                               "Missed Shot" = "MS", 
                               "Blocked" = "B", 
                               "Lost Ball" = "LB",
                               "Out" = "O", 
                               "Free Hit" = "FH", 
                               "Rebound" = "R"))

#rename all columns with prefix AC_E#
colnames(t_ace) <- paste("AC_E", colnames(t_ace), sep = "_")

return(t_ace)
  } else {(t_ace <- setNames(as.data.frame(matrix(c(0, 0, 0, 0, 0, 0, 0, 0, 0), ncol = 9, nrow = 1)), c("AC_E_T", "AC_E_I", "AC_E_MT", "AC_E_MS", "AC_E_B", "AC_E_LB", "AC_E_O", "AC_E_FH", "AC_E_R")))
    
  return(t_ace)}
}

t_ace_list <- list()
for (i in seq_along(turnovers_e_list)){
  t_ace_list[[i]] <- t_ace_f(turnovers_e_list[[i]])
}
Create table to identify turnover types in A25 in established attacks and convert to percentages for comparison
# Turnovers Established Attack A25 #
t_a25e_f <- function(x){
  if (any(grepl("A25", x$TO.Location))){
# extract from Turnovers data frame only rows referring to A25 and save as separate data frame #
t_a25e <- filter(x, TO.Location == "A25")

# create table with Teams as rows and Turnover types as columns #
t_a25e <- table(t_a25e$Team, t_a25e$Type)

# transform to percentages (% turnover types used in A25) #
t_a25e <- round(prop.table(t_a25e) * 100, digits = 2)

# convert to data frame #
t_a25e <- as.data.frame.matrix(t_a25e)

# list of turnover types #
m <- c("Tackle", "Intercept", "Miss Trap","Blocked", "Lost Ball", "Out", "Free Hit", "Rebound")

# check to see if all data frames contain all columns, if not add column=0 #
t_a25e[m[!(m %in% colnames(t_a25e))]] = 0

# rename columns #
t_a25e <- plyr::rename(t_a25e, c("Tackle" = "T", 
                                 "Intercept" = "I", 
                                 "Miss Trap" = "MT",
                                 "Blocked" = "B", 
                                 "Lost Ball" = "LB", 
                                 "Out" = "O", 
                                 "Free Hit" = "FH", 
                                 "Rebound" = "R"))

# rename all columns with prefix A25_E #
colnames(t_a25e) <- paste("A25_E", colnames(t_a25e), sep = "_")

return(t_a25e)
  } else {(t_a25e <- setNames(as.data.frame(matrix(c(0, 0, 0, 0, 0, 0, 0, 0), ncol = 8, nrow = 1)), c("A25_E_T", "A25_E_I", "A25_E_MT", "A25_E_B", "A25_E_LB", "A25_E_O", "A25_E_FH", "A25_E_R")))
    
  return(t_a25e)}
}

t_a25e_list <- list()
for (i in seq_along(turnovers_e_list)){
  t_a25e_list[[i]] <- t_a25e_f(turnovers_e_list[[i]])
}
Create table to identify turnover types in A50 in established attacks and convert to percentages for comparison
# Turnovers Established Attack A50 #
t_a50e_f <- function(x){
  if (any(grepl("A50", x$TO.Location))){
# extract from Turnovers data frame only rows referring to A50 and save as separate data frame #
t_a50e <- filter(x, TO.Location == "A50")

# create table with Teams as rows and Turnover types as columns #
t_a50e <- table(t_a50e$Team, t_a50e$Type)

# transform to percentages (% turnover types used in A50) #
t_a50e <- round(prop.table(t_a50e) * 100, digits = 2)

# convert to data frame #
t_a50e <- as.data.frame.matrix(t_a50e)

# list of turnover types #
m <- c("Tackle", "Intercept", "Miss Trap","Blocked", "Lost Ball", "Out", "Free Hit", "Rebound")

# check to see if all data frames contain all columns, if not add column=0 #
t_a50e[m[!(m %in% colnames(t_a50e))]] = 0

# rename columns #
t_a50e <- plyr::rename(t_a50e, c("Tackle" = "T", 
                                 "Intercept" = "I", 
                                 "Miss Trap" = "MT",
                                 "Blocked" = "B", 
                                 "Lost Ball" = "LB", 
                                 "Out" = "O", 
                                 "Free Hit" = "FH", 
                                 "Rebound" = "R"))

# rename all columns with prefix A50_E #
colnames(t_a50e) <- paste("A50_E", colnames(t_a50e), sep = "_")

return(t_a50e)
  } else {(t_a50e <- setNames(as.data.frame(matrix(c(0, 0, 0, 0, 0, 0, 0, 0), ncol = 8, nrow = 1)), c("A50_E_T", "A50_E_I", "A50_E_MT", "A50_E_B", "A50_E_LB", "A50_E_O", "A50_E_FH", "A50_E_R")))
    
  return(t_a50e)}
}

t_a50e_list <- list()
for (i in seq_along(turnovers_e_list)){
  t_a50e_list[[i]] <- t_a50e_f(turnovers_e_list[[i]])
}
Create table to identify turnover types in D50 in established attacks and convert to percentages for comparison
# Turnovers Established Attack D50 #
t_d50e_f <- function(x){
  if (any(grepl("D50", x$TO.Location))){
# extract from Turnovers data frame only rows referring to D50 and save as separate data frame #
t_d50e <- filter(x, TO.Location == "D50")

# create table with Teams as rows and Turnover types as columns #
t_d50e <- table(t_d50e$Team, t_d50e$Type)

# transform to percentages (% turnover types used in D50) #
t_d50e <- round(prop.table(t_d50e) * 100, digits = 2)

# convert to data frame #
t_d50e <- as.data.frame.matrix(t_d50e)

# list of turnover types #
m <- c("Tackle", "Intercept", "Miss Trap","Blocked", "Lost Ball", "Out", "Free Hit")

# check to see if all data frames contain all columns, if not add column=0 #
t_d50e[m[!(m %in% colnames(t_d50e))]] = 0

# rename columns #
t_d50e <- plyr::rename(t_d50e, c("Tackle" = "T", 
                                 "Intercept" = "I", 
                                 "Miss Trap" = "MT",
                                 "Blocked" = "B", 
                                 "Lost Ball" = "LB", 
                                 "Out" = "O", 
                                 "Free Hit" = "FH"))

# rename all columns with prefix D50_E #
colnames(t_d50e) <- paste("D50_E", colnames(t_d50e), sep = "_")

return(t_d50e)
  } else {(t_d50e <- setNames(as.data.frame(matrix(c(0, 0, 0, 0, 0, 0, 0), ncol = 7,  nrow = 1)), c("D50_E_T", "D50_E_I", "D50_E_MT", "D50_E_B", "D50_E_LB", "D50_E_O", "D50_E_FH")))
    
  return(t_d50e)}
}

t_d50e_list <- list()
for (i in seq_along(turnovers_e_list)){
  t_d50e_list[[i]] <- t_d50e_f(turnovers_e_list[[i]])
}
Create table to identify turnover types in D25 in established attacks and convert to percentages for comparison
# Turnovers Established Attack D25 #
t_d25e_f <- function(x){
  if (any(grepl("D25", x$TO.Location))){
# extract from Turnovers data frame only rows referring to D25 and save as separate data frame #
t_d25e <- filter(x, TO.Location == "D25")

# create table with Teams as rows and Turnover types as columns #
t_d25e <- table(t_d25e$Team, t_d25e$Type)

# transform to percentages (% turnover types used in D25) #
t_d25e <- round(prop.table(t_d25e) * 100, digits = 2)

# convert to data frame #
t_d25e <- as.data.frame.matrix(t_d25e)

# list of turnover types #
m <- c("Tackle", "Intercept", "Miss Trap","Blocked", "Lost Ball", "Out", "Free Hit")

# check to see if all data frames contain all columns, if not add column=0 #
t_d25e[m[!(m %in% colnames(t_d25e))]] = 0

# rename columns #
t_d25e <- plyr::rename(t_d25e, c("Tackle" = "T", 
                                 "Intercept" = "I", 
                                 "Miss Trap" = "MT",
                                 "Blocked" = "B", 
                                 "Lost Ball" = "LB", 
                                 "Out" = "O", 
                                 "Free Hit" = "FH"))

# rename all columns with prefix D25_E #
colnames(t_d25e) <- paste("D25_E", colnames(t_d25e), sep = "_")


return(t_d25e)
  } else {(t_d25e <- setNames(as.data.frame(matrix(c(0, 0, 0, 0, 0, 0, 0), ncol = 7, nrow = 1)), c("D25_E_T", "D25_E_I", "D25_E_MT", "D25_E_B", "D25_E_LB", "D25_E_O", "D25_E_FH")))
    
  return(t_d25e)}
}

t_d25e_list <- list()
for (i in seq_along(turnovers_e_list)){
  t_d25e_list[[i]] <- t_d25e_f(turnovers_e_list[[i]])
}
Create table to identify turnover types in AC in counter attacks and convert to percentages for comparison
# Turnovers Counter Attack AC #
t_acc_f <- function(x){
  if (any(grepl("AC", x$TO.Location))){
# extract from Turnovers data frame only rows referring to AC and save as separate data frame #
t_acc <- filter(x, TO.Location == "AC")

# create table with Teams as rows and Turnover types as columns #
t_acc <- table(t_acc$Team, t_acc$Type)

# transform to percentages (% turnover types used in AC) #
t_acc <- round(prop.table(t_acc) * 100, digits = 2)

# convert to data frame #
t_acc <- as.data.frame.matrix(t_acc)

# list of turnover types #
m <- c("Tackle", "Intercept", "Miss Trap", "Missed Shot", "Blocked", "Lost Ball", "Out", "Free Hit", "Rebound")

# check to see if all data frames contain all columns, if not add column=0 #
t_acc[m[!(m %in% colnames(t_acc))]] = 0

# rename columns #
t_acc <- plyr::rename(t_acc, c("Tackle" = "T", 
                               "Intercept" = "I", 
                               "Miss Trap" = "MT",
                               "Missed Shot" = "MS", 
                               "Blocked" = "B", 
                               "Lost Ball" = "LB", 
                               "Out" = "O", 
                               "Free Hit" = "FH", 
                               "Rebound" = "R"))

# rename all columns with prefix AC_C #
colnames(t_acc) <- paste("AC_C", colnames(t_acc), sep = "_")


return(t_acc)
  } else {(t_acc <- setNames(as.data.frame(matrix(c(0, 0, 0, 0, 0, 0, 0, 0, 0), ncol = 9, nrow = 1)), c("AC_C_T", "AC_C_I", "AC_C_MT", "AC_C_MS", "AC_C_B", "AC_C_LB", "AC_C_O", "AC_C_FH", "AC_C_R")))
    
  return(t_acc)}
}

t_acc_list <- list()
for (i in seq_along(turnovers_c_list)){
  t_acc_list[[i]] <- t_acc_f(turnovers_c_list[[i]])
}
Create table to identify turnover types in A25 in counter attacks and convert to percentages for comparison
# Turnovers Counter Attack A25 #
t_a25c_f <- function(x){
  if (any(grepl("A25", x$TO.Location))){
# extract from Turnovers data frame only rows referring to A25 and save as separate data frame #
t_a25c <- filter(x, TO.Location == "A25")

# create table with Teams as rows and Turnover types as columns #
t_a25c <- table(t_a25c$Team, t_a25c$Type)

# transform to percentages (% turnover types used in A25) #
t_a25c <- round(prop.table(t_a25c) * 100, digits = 2)

# convert to data frame #
t_a25c <- as.data.frame.matrix(t_a25c)

# list of turnover types #
m <- c("Tackle", "Intercept", "Miss Trap","Blocked", "Lost Ball", "Out", "Free Hit", "Rebound")

# check to see if all data frames contain all columns, if not add column=0 #
t_a25c[m[!(m %in% colnames(t_a25c))]] = 0

# rename columns #
t_a25c <- plyr::rename(t_a25c, c("Tackle" = "T", 
                                 "Intercept" = "I", 
                                 "Miss Trap" = "MT",
                                 "Blocked" = "B", 
                                 "Lost Ball" = "LB", 
                                 "Out" = "O", 
                                 "Free Hit" = "FH", 
                                 "Rebound" = "R"))

# rename all columns with prefix A25_C #
colnames(t_a25c) <- paste("A25_C", colnames(t_a25c), sep = "_")

return(t_a25c)
  } else {(t_a25c <- setNames(as.data.frame(matrix(c(0, 0, 0, 0, 0, 0, 0, 0), ncol = 8, nrow = 1)), c("A25_C_T", "A25_C_I", "A25_C_MT", "A25_C_B", "A25_C_LB", "A25_C_O", "A25_C_FH", "A25_C_R")))
    
  return(t_a25c)}
}

t_a25c_list <- list()
for (i in seq_along(turnovers_c_list)){
  t_a25c_list[[i]] <- t_a25c_f(turnovers_c_list[[i]])
}
Create table to identify turnover types in A50 in counter attacks and convert to percentages for comparison
# Turnovers Counter Attack A50 #
t_a50c_f <- function(x){
  if (any(grepl("A50", x$TO.Location))){
# extract from Turnovers data frame only rows referring to A50 and save as separate data frame #
t_a50c <- filter(x, TO.Location == "A50")

# create table with Teams as rows and Turnover types as columns #
t_a50c <- table(t_a50c$Team, t_a50c$Type)

# transform to percentages (% turnover types used in A50) #
t_a50c <- round(prop.table(t_a50c) * 100, digits = 2)

# convert to data frame #
t_a50c <- as.data.frame.matrix(t_a50c)

# list of turnover types #
m <- c("Tackle", "Intercept", "Miss Trap","Blocked", "Lost Ball", "Out", "Free Hit", "Rebound")

# check to see if all data frames contain all columns, if not add column=0 #
t_a50c[m[!(m %in% colnames(t_a50c))]] = 0

# rename columns #
t_a50c <- plyr::rename(t_a50c, c("Tackle" = "T", 
                                 "Intercept" = "I", 
                                 "Miss Trap" = "MT",
                                 "Blocked" = "B", 
                                 "Lost Ball" = "LB", 
                                 "Out" = "O", 
                                 "Free Hit" = "FH", 
                                 "Rebound" = "R"))

#rename all columns with prefix A50_C#
colnames(t_a50c) <- paste("A50_C", colnames(t_a50c), sep = "_")

return(t_a50c)
  } else {(t_a50c <- setNames(as.data.frame(matrix(c(0, 0, 0, 0, 0, 0, 0, 0), ncol = 8, nrow = 1)), c("A50_C_T", "A50_C_I", "A50_C_MT", "A50_C_B", "A50_C_LB", "A50_C_O", "A50_C_FH", "A50_C_R")))
    
  return(t_a50c)}
}

t_a50c_list <- list()
for (i in seq_along(turnovers_c_list)){
  t_a50c_list[[i]] <- t_a50c_f(turnovers_c_list[[i]])
}
Create table to identify turnover types in D50 in counter attacks and convert to percentages for comparison
# Turnovers Counter Attack D50 #
t_d50c_f <- function(x){
  if (any(grepl("D50", x$TO.Location))){
# extract from Turnovers data frame only rows referring to D50 and save as separate data frame #
t_d50c <- filter(x, TO.Location == "D50")

# create table with Teams as rows and Turnover types as columns #
t_d50c <- table(t_d50c$Team, t_d50c$Type)

# transform to percentages (% turnover types used in D50) #
t_d50c <- round(prop.table(t_d50c) * 100, digits = 2)

# convert to data frame #
t_d50c <- as.data.frame.matrix(t_d50c)

# list of turnover types #
m <- c("Tackle", "Intercept", "Miss Trap","Blocked", "Lost Ball", "Out", "Free Hit")

# check to see if all data frames contain all columns, if not add column=0 #
t_d50c[m[!(m %in% colnames(t_d50c))]] = 0

# rename columns #
t_d50c <- plyr::rename(t_d50c, c("Tackle" = "T", 
                                 "Intercept" = "I", 
                                 "Miss Trap" = "MT",
                                 "Blocked" = "B", 
                                 "Lost Ball" = "LB", 
                                 "Out" = "O", 
                                 "Free Hit" = "FH"))

# rename all columns with prefix D50_C #
colnames(t_d50c) <- paste("D50_C", colnames(t_d50c), sep = "_")

return(t_d50c)
  } else {(t_d50c <- setNames(as.data.frame(matrix(c(0, 0, 0, 0, 0, 0, 0), ncol = 7, nrow = 1)), c("D50_C_T", "D50_C_I", "D50_C_MT", "D50_C_B", "D50_C_LB", "D50_C_O", "D50_C_FH")))
    
  return(t_d50c)}
}

t_d50c_list <- list()
for (i in seq_along(turnovers_c_list)){
  t_d50c_list[[i]] <- t_d50c_f(turnovers_c_list[[i]])
}
Create table to identify turnover types in D25 in counter attacks and convert to percentages for comparison
# Turnovers Counter Attack D25 #
t_d25c_f <- function(x){
  if (any(grepl("D25", x$TO.Location))){
# extract from Turnovers data frame only rows referring to D25 and save as separate data frame #
t_d25c <- filter(x, TO.Location == "D25")

# create table with Teams as rows and Turnover types as columns #
t_d25c <- table(t_d25c$Team, t_d25c$Type)

# transform to percentages (% turnover types used in D25) #
t_d25c <- round(prop.table(t_d25c) * 100, digits = 2)

# convert to data frame #
t_d25c <- as.data.frame.matrix(t_d25c)

# list of turnover types #
m <- c("Tackle", "Intercept", "Miss Trap","Blocked", "Lost Ball", "Out", "Free Hit")

# check to see if all data frames contain all columns, if not add column=0 #
t_d25c[m[!(m %in% colnames(t_d25c))]] = 0

# rename columns #
t_d25c <- plyr::rename(t_d25c, c("Tackle" = "T", 
                                 "Intercept" = "I", 
                                 "Miss Trap" = "MT",
                                 "Blocked" = "B", 
                                 "Lost Ball" = "LB", 
                                 "Out" = "O", 
                                 "Free Hit" = "FH"))

# rename all columns with prefix D25_C #
colnames(t_d25c) <- paste("D25_C", colnames(t_d25c), sep = "_")

return(t_d25c)
  } else {(t_d25c <- setNames(as.data.frame(matrix(c(0, 0, 0, 0, 0, 0, 0), ncol = 7, nrow = 1)), c("D25_C_T", "D25_C_I", "D25_C_MT", "D25_C_B", "D25_C_LB", "D25_C_O", "D25_C_FH")))
    
  return(t_d25c)}
}

t_d25c_list <- list()
for (i in seq_along(turnovers_c_list)){
  t_d25c_list[[i]] <- t_d25c_f(turnovers_c_list[[i]])
}
Join all teams turnover types data frames per location into one
# Movement Effects per location per team #
t_list <- list()
for (i in seq_along(game_list)){
  t_list[[i]] <- bind_cols(t_ace_list[[i]], t_a25e_list[[i]], t_a50e_list[[i]], t_d50e_list[[i]], t_d25e_list[[i]],  t_acc_list[[i]], t_a25c_list[[i]], t_a50c_list[[i]], t_d50c_list[[i]], t_d25c_list[[i]])
}

t_list<-bind_rows(t_list)

11. EXPORT DATA

Calculate the league average for all teams turnover types and tidy table for presentation
# add team names #
t_list <- rownames_to_column(t_list, "Team")

# calculate average per variable #
t_avg <- data.frame(Percent = colMeans(t_list[, c(-1)]))

# add variables names #
t_avg <- rownames_to_column(t_avg, "Variable")
t_avg <- separate(t_avg, Variable, c("Location", "Attack", "Turnover"))

# change to factor and reorder #
t_avg$Turnover <- factor(t_avg$Turnover, levels = c("B", "FH", "I", "LB", "MS", "MT", "O", "R", "T"), labels = c("Blocked", "Free Hit", "Intercept", "Lost Ball", "Missed Shot", "Miss Trap", "Out", "Rebound", "Tackle"))

t_avg$Location <- factor(t_avg$Location, levels = c("AC", "A25", "A50", "D50", "D25"))


# save data frame #
write_xlsx(t_avg, "Data/Processed Data//Turnovers_Avg_Women.xlsx")

t_avg2 <- head(t_avg)
knitr::kable(t_avg2, caption = "League Turnover Type Averages")
League Turnover Type Averages
Location Attack Turnover Percent
AC E Blocked 8.007778
AC E Free Hit 17.262222
AC E Intercept 22.965556
AC E Lost Ball 5.890000
AC E Miss Trap 18.314444
AC E Missed Shot 6.182222
Calculate team averages for turnover types and tidy table for presentation
# transform from wide to long #
t_list_long <- t_list %>% 
  pivot_longer(cols = AC_E_B:D25_C_T,
               names_to = "Variable",
               values_to = "Percent")

# separate descriptive column #
t_list_long <- separate(t_list_long, Variable, c("Location", "Attack", "Turnover"))

# change to factor and reorder #
t_list_long$Turnover <- factor(t_list_long$Turnover, levels = c("B", "FH", "I", "LB", "MS", "MT", "O", "R", "T"), labels = c("Blocked", "Free Hit", "Intercept", "Lost Ball", "Missed Shot", "Miss Trap", "Out", "Rebound", "Tackle"))

t_list_long$Location <- factor(t_list_long$Location, levels = c("AC", "A25", "A50", "D50", "D25"))


#save data frame#
write_xlsx(t_list_long, "Data/Processed Data//Turnovers_Women.xlsx")

t_list_long2 <- head(t_list_long)
knitr::kable(t_list_long2, caption = "Team Turnover Type Averages")
Team Turnover Type Averages
Team Location Attack Turnover Percent
Argentina AC E Blocked 6.46
Argentina AC E Free Hit 16.73
Argentina AC E Intercept 19.77
Argentina AC E Lost Ball 5.70
Argentina AC E Miss Trap 14.83
Argentina AC E Missed Shot 7.22

GOAL SHOTS OPEN PLAY

12. FILTER

Extract goal shot rows, select and rename descriptive columns
goal_shots_f <- function(x){
# extract from all data (Game) rows referring to Goal Shots and save as separate data frame (Goal_Shots) #
goal_shots <- filter(x, A == "Goal")

# rename columns #
goal_shots <- plyr::rename(goal_shots, c("...7" = "GS.Location", 
                                         "...8" = "GS.Pressure",
                                         "...9" = "Outcome", 
                                         "...10" = "Match.Status", 
                                         "...11" = "Match.Location", 
                                         "...12" = "Quality.Opp", 
                                         "...13" = "Location", 
                                         "...14" = "Attack.Type"))

goal_shots <- filter(goal_shots, Attack.Type != "Set Piece")

# change locations to factors #
goal_shots$GS.Location <- as.factor(goal_shots$GS.Location)
goal_shots$GS.Location <- revalue(goal_shots$GS.Location, c("1" = "Goal_Line", 
                                                            "2" = "Close_Outer_Right", 
                                                            "3" = "Close_Inner_Right", 
                                                            "4" = "Close_Center", 
                                                            "5" = "Close_Inner_Left", 
                                                            "6" = "Close_Outer_Left", 
                                                            "7" = "Middle_Outer_Right", 
                                                            "8" = "Middle_Inner_Right", 
                                                            "9" = "Middle_Center", 
                                                            "10" = "Middle_Inner_Left", 
                                                            "11" = "Middle_Outer_Left", 
                                                            "12" = "Far_Outer_Right", 
                                                            "13" = "Far_Inner_Right", 
                                                            "14" = "Far_Center", 
                                                            "15" = "Far_Inner_Left", 
                                                            "16" = "Far_Outer_Left"))


# remove unnecessary columns #
goal_shots <- select(goal_shots, Team, Match_Team, Opposition, Gender, GS.Location, GS.Pressure, Outcome, Match.Status, Match.Location, Quality.Opp, Location, Attack.Type)

return(goal_shots)
}

goal_shots_list <- list()
for (i in seq_along(game_list)){
  goal_shots_list[[i]] <- goal_shots_f(game_list[[i]])
}

13. CALCULATE GOAL SHOT LOCATIONS

Create table to identify goal shot locations per team and convert to percentages
# Goal Shots Locations #
loc_f <- function(x){
   
# create table with Teams as rows and Goal Shot locations as columns #
loc <- table(x$Team, x$GS.Location)

# transform to percentages (% locations) #
loc <- round(prop.table(loc) * 100, digits = 2)

# convert to data frame #
loc <- as.data.frame.matrix(loc)

# list of goal shot locations #
r <- c("Goal_Line","Close_Outer_Left", "Close_Inner_Left", "Close_Center", "Close_Inner_Right", "Close_Outer_Right", "Middle_Outer_Left", "Middle_Inner_Left", "Middle_Center", "Middle_Inner_Right", "Middle_Outer_Right", "Far_Outer_Left", "Far_Inner_Left", "Far_Center", "Far_Inner_Right", "Far_Outer_Right")

# check to see if all data frames contain all columns, if not add column=0 #
loc[r[!(r %in% colnames(loc))]] = 0

# add teams #
loc <- rownames_to_column(loc, "Team")

return(loc)
}
  
loc_list <- list()
for (i in seq_along(goal_shots_list)){
  loc_list[[i]] <- loc_f(goal_shots_list[[i]])
}

14. CALCULATE GOAL SHOT OUTCOMES PER LOCATION

Create table to identify goal shot outcomes from goal line and convert to percentages
# Goal Shots Outcomes Goal Line#
gl_f <- function(x){
   if (any(grepl("Goal_Line", x$GS.Location))){
# extract from Goal_Shots data frame only rows referring to Goal Line and save as separate data frame #
gl <- filter(x, GS.Location == "Goal_Line")

# create table with Teams as rows and Goal Shot Outcomes as columns #
gl <- table(gl$Team, gl$Outcome)

# transform to percentages (% GS Outcomes from Goal Line) #
gl <- round(prop.table(gl) * 100, digits = 2)

# convert to data frame #
gl <- as.data.frame.matrix(gl)

# list of goal shot outcomes #
q <- c("Goal", "Saved", "Smothered", "Turnover")

# check to see if all data frames contain all columns, if not add column=0 #
gl[q[!(q %in% colnames(gl))]] = 0

# rename all columns with prefix GL #
colnames(gl)<-paste("GL", colnames(gl), sep = "_")

return(gl)
} else {(gl <- setNames(as.data.frame(matrix(c(0, 0, 0, 0), ncol = 4, nrow = 1)), c("GL_Goal", "GL_Saved", "GL_Smothered", "GL_Turnover"))) 
  return(gl)}
}

gl_list <- list()
for (i in seq_along(goal_shots_list)){
 gl_list[[i]] <- gl_f(goal_shots_list[[i]])
}
Create table to identify goal shot outcomes from close outer right and convert to percentages
# Goal Shots Outcomes Close Outer Right #
cor_f <- function(x){
   if (any(grepl("Close_Outer_Right", x$GS.Location))){
# extract from Goal_Shots data frame only rows referring to Close Outer Right and save as separate data frame #
cor <- filter(x, GS.Location == "Close_Outer_Right")

# create table with Teams as rows and Goal Shot Outcomes as columns #
cor <- table(cor$Team, cor$Outcome)

# transform to percentages (% GS Outcomes from Close Outer Right) #
cor <- round(prop.table(cor) * 100, digits = 2)

# convert to data frame #
cor <- as.data.frame.matrix(cor)

# list of goal shot outcomes #
q <- c("Goal", "Saved", "Smothered", "Turnover")

# check to see if all data frames contain all columns, if not add column=0 #
cor[q[!(q %in% colnames(cor))]] = 0

# rename all columns with prefix COR #
colnames(cor) <- paste("COR", colnames(cor), sep = "_")

return(cor)
} else {(cor <- setNames(as.data.frame(matrix(c(0, 0, 0, 0), ncol = 4, nrow = 1)), c("COR_Goal", "COR_Saved", "COR_Smothered", "COR_Turnover"))) 
  return(cor)}
}

cor_list <- list()
for (i in seq_along(goal_shots_list)){
 cor_list[[i]] <- cor_f(goal_shots_list[[i]])
}
Create table to identify goal shot outcomes from close inner right and convert to percentages
# Goal Shots Outcomes Close Inner Right #
cir_f <- function(x){
   if (any(grepl("Close_Inner_Right", x$GS.Location))){
# extract from Goal_Shots data frame only rows referring to Close Inner Right and save as separate data frame #
cir <- filter(x, GS.Location == "Close_Inner_Right")

# create table with Teams as rows and Goal Shot Outcomes as columns #
cir <- table(cir$Team, cir$Outcome)

# transform to percentages (% GS Outcomes from Close Inner Right) #
cir <- round(prop.table(cir) * 100, digits = 2)

# convert to data frame #
cir <- as.data.frame.matrix(cir)

# list of goal shot outcomes #
q <- c("Goal", "Saved", "Smothered", "Turnover")

# check to see if all data frames contain all columns, if not add column=0 #
cir[q[!(q %in% colnames(cir))]] = 0


# rename all columns with prefix CIR #
colnames(cir) <- paste("CIR", colnames(cir), sep = "_")

return(cir)
} else {(cir <- setNames(as.data.frame(matrix(c(0, 0, 0, 0), ncol = 4, nrow = 1)), c("CIR_Goal", "CIR_Saved", "CIR_Smothered", "CIR_Turnover"))) 
  return(cir)}
}

cir_list <- list()
for (i in seq_along(goal_shots_list)){
 cir_list[[i]] <- cir_f(goal_shots_list[[i]])
}
Create table to identify goal shot outcomes from close center and convert to percentages
# Goal Shots Outcomes Close Center #
cc_f <- function(x){
   if (any(grepl("Close_Center", x$GS.Location))){
# extract from Goal_Shots data frame only rows referring to Close Center and save as separate data frame #
cc <- filter(x, GS.Location == "Close_Center")

# create table with Teams as rows and Goal Shot Outcomes as columns #
cc <- table(cc$Team, cc$Outcome)

# transform to percentages (% GS Outcomes from Close Center) #
cc <- round(prop.table(cc) * 100, digits = 2)

# convert to data frame #
cc <- as.data.frame.matrix(cc)

# list of goal shot outcomes #
q <- c("Goal", "Saved", "Smothered", "Turnover")

# check to see if all data frames contain all columns, if not add column=0 #
cc[q[!(q %in% colnames(cc))]] = 0

# rename all columns with prefix CC #
colnames(cc) <- paste("CC", colnames(cc), sep = "_")

return(cc)
} else {(cc <- setNames(as.data.frame(matrix(c(0, 0, 0, 0), ncol = 4, nrow = 1)), c("CC_Goal", "CC_Saved", "CC_Smothered", "CC_Turnover"))) 
  return(cc)}
}

cc_list <- list()
for (i in seq_along(goal_shots_list)){
 cc_list[[i]] <- cc_f(goal_shots_list[[i]])
}
Create table to identify goal shot outcomes from close inner left and convert to percentages
# Goal Shots Outcomes Close Inner Left #
cil_f <- function(x){
   if (any(grepl("Close_Inner_Left", x$GS.Location))){
# extract from Goal_Shots data frame only rows referring to Close Inner Left and save as separate data frame #
cil <- filter(x, GS.Location == "Close_Inner_Left")

# create table with Teams as rows and Goal Shot Outcomes as columns #
cil <- table(cil$Team, cil$Outcome)

# transform to percentages (% GS Outcomes from Close Inner Left) #
cil <- round(prop.table(cil) * 100, digits = 2)

# convert to data frame #
cil <- as.data.frame.matrix(cil)

# list of goal shot outcomes #
q <- c("Goal", "Saved", "Smothered", "Turnover")

# check to see if all data frames contain all columns, if not add column=0 #
cil[q[!(q %in% colnames(cil))]] = 0

# rename all columns with prefix  CIL #
colnames(cil) <- paste("CIL", colnames(cil), sep = "_")

return(cil)
} else {(cil <- setNames(as.data.frame(matrix(c(0, 0, 0, 0), ncol = 4, nrow = 1)), c("CIL_Goal", "CIL_Saved", "CIL_Smothered", "CIL_Turnover"))) 
  return(cil)}
}

cil_list <-list()
for (i in seq_along(goal_shots_list)){
 cil_list[[i]] <- cil_f(goal_shots_list[[i]])
}
Create table to identify goal shot outcomes from close outer left and convert to percentages
# Goal Shots Outcomes Close Outer Left #
col_f <- function(x){
   if (any(grepl("Close_Outer_Left", x$GS.Location))){
# extract from Goal_Shots data frame only rows referring to Close Outer Left and save as separate data frame #
col <- filter(x, GS.Location == "Close_Outer_Left")

# create table with Teams as rows and Goal Shot Outcomes as columns #
col <- table(col$Team, col$Outcome)

# transform to percentages (% GS Outcomes from Close Outer Left) #
col <- round(prop.table(col) * 100, digits = 2)

# convert to data frame #
col <- as.data.frame.matrix(col)

# list of goal shot outcomes #
q <- c("Goal", "Saved", "Smothered", "Turnover")

# check to see if all data frames contain all columns, if not add column=0 #
col[q[!(q %in% colnames(col))]] = 0

# rename all columns with prefix  COL #
colnames(col) <- paste("COL", colnames(col), sep = "_")

return(col)
} else {(col <- setNames(as.data.frame(matrix(c(0, 0, 0, 0), ncol = 4, nrow = 1)), c("COL_Goal", "COL_Saved", "COL_Smothered", "COL_Turnover"))) 
  return(col)}
}

col_list <- list()
for (i in seq_along(goal_shots_list)){
 col_list[[i]] <- col_f(goal_shots_list[[i]])
}
Create table to identify goal shot outcomes from middle outer right and convert to percentages
# Goal Shots Outcomes Middle Outer Right #
mor_f <- function(x){
   if (any(grepl("Middle_Outer_Right", x$GS.Location))){
# extract from Goal_Shots data frame only rows referring to Middle Outer Right and save as separate data frame #
mor <- filter(x, GS.Location == "Middle_Outer_Right")

# create table with Teams as rows and Goal Shot Outcomes as columns #
mor <- table(mor$Team, mor$Outcome)

# transform to percentages (% GS Outcomes from Middle Outer Right) #
mor <- round(prop.table(mor) * 100, digits = 2)

# convert to data frame #
mor <- as.data.frame.matrix(mor)

# list of goal shot outcomes #
q <- c("Goal", "Saved", "Smothered", "Turnover")

# check to see if all data frames contain all columns, if not add column=0 #
mor[q[!(q %in% colnames(mor))]] = 0

# rename all columns with prefix MOR #
colnames(mor) <- paste("MOR", colnames(mor), sep = "_")

return(mor)
} else {(mor <- setNames(as.data.frame(matrix(c(0, 0, 0, 0), ncol = 4, nrow = 1)), c("MOR_Goal", "MOR_Saved", "MOR_Smothered", "MOR_Turnover"))) 
  return(mor)}
}

mor_list <- list()
for (i in seq_along(goal_shots_list)){
 mor_list[[i]] <- mor_f(goal_shots_list[[i]])
}
Create table to identify goal shot outcomes from middle inner right and convert to percentages
# Goal Shots Outcomes Middle Inner Right #
mir_f <- function(x){
   if (any(grepl("Middle_Inner_Right", x$GS.Location))){
# extract from Goal_Shots data frame only rows referring to Middle Inner Right and save as separate data frame #
mir <- filter(x, GS.Location == "Middle_Inner_Right")

# create table with Teams as rows and Goal Shot Outcomes as columns #
mir <- table(mir$Team, mir$Outcome)

# transform to percentages (% GS Outcomes from Middle Inner Right) #
mir <- round(prop.table(mir) * 100, digits = 2)

# convert to data frame #
mir <- as.data.frame.matrix(mir)

# list of goal shot outcomes #
q <- c("Goal", "Saved", "Smothered", "Turnover")

# check to see if all data frames contain all columns, if not add column=0 #
mir[q[!(q %in% colnames(mir))]] = 0

# rename all columns with prefix MIR # 
colnames(mir) <- paste("MIR", colnames(mir), sep = "_")

return(mir)
} else {(mir <- setNames(as.data.frame(matrix(c(0, 0, 0, 0), ncol = 4, nrow = 1)), c("MIR_Goal", "MIR_Saved", "MIR_Smothered", "MIR_Turnover"))) 
  return(mir)}
}

mir_list <- list()
for (i in seq_along(goal_shots_list)){
 mir_list[[i]] <- mir_f(goal_shots_list[[i]])
}
Create table to identify goal shot outcomes from middle center and convert to percentages
# Goal Shots Outcomes Middle Center #
mc_f <- function(x){
   if (any(grepl("Middle_Center", x$GS.Location))){
# extract from Goal_Shots data frame only rows referring to Middle Center and save as separate data frame #
mc <- filter(x, GS.Location == "Middle_Center")

# create table with Teams as rows and Goal Shot Outcomes as columns #
mc <- table(mc$Team, mc$Outcome)

# transform to percentages (% GS Outcomes from Middle Center) #
mc <- round(prop.table(mc) * 100, digits = 2)

# convert to data frame #
mc <- as.data.frame.matrix(mc)

# list of goal shot outcomes #
q <- c("Goal", "Saved", "Smothered", "Turnover")

# check to see if all data frames contain all columns, if not add column=0 #
mc[q[!(q %in% colnames(mc))]] = 0

# rename all columns with prefix MC #
colnames(mc) <- paste("MC", colnames(mc), sep = "_")

return(mc)
} else {(mc <- setNames(as.data.frame(matrix(c(0, 0, 0, 0), ncol = 4, nrow = 1)), c("MC_Goal", "MC_Saved", "MC_Smothered", "MC_Turnover"))) 
  return(mc)}
}

mc_list <- list()
for (i in seq_along(goal_shots_list)){
 mc_list[[i]] <- mc_f(goal_shots_list[[i]])
}
Create table to identify goal shot outcomes from middle inner left and convert to percentages
# Goal Shots Outcomes Middle Inner Left #
mil_f <- function(x){
   if (any(grepl("Middle_Inner_Left", x$GS.Location))){
# extract from Goal_Shots data frame only rows referring to Middle Inner Left and save as separate data frame #
mil <- filter(x, GS.Location == "Middle_Inner_Left")

# create table with Teams as rows and Goal Shot Outcomes as columns #
mil <- table(mil$Team, mil$Outcome)

# transform to percentages (% GS Outcomes from Middle Inner Left) #
mil <- round(prop.table(mil) * 100, digits = 2)

# convert to data frame #
mil <- as.data.frame.matrix(mil)

# list of goal shot outcomes #
q <- c("Goal", "Saved", "Smothered", "Turnover")

# check to see if all data frames contain all columns, if not add column=0 #
mil[q[!(q %in% colnames(mil))]] = 0

# rename all columns with prefix MIL #
colnames(mil) <- paste("MIL", colnames(mil), sep = "_")

return(mil)
} else {(mil <- setNames(as.data.frame(matrix(c(0, 0, 0, 0), ncol = 4, nrow = 1)), c("MIL_Goal", "MIL_Saved", "MIL_Smothered", "MIL_Turnover"))) 
  return(mil)}
}

mil_list <- list()
for (i in seq_along(goal_shots_list)){
 mil_list[[i]] <- mil_f(goal_shots_list[[i]])
}
Create table to identify goal shot outcomes from middle outer left and convert to percentages
# Goal Shots Outcomes Middle Outer Left #
mol_f <- function(x){
   if (any(grepl("Middle_Outer_Left", x$GS.Location))){
# extract from Goal_Shots data frame only rows referring to Middle Outer Left and save as separate data frame #
mol <- filter(x, GS.Location == "Middle_Outer_Left")

# create table with Teams as rows and Goal Shot Outcomes as columns #
mol <- table(mol$Team, mol$Outcome)

# transform to percentages (% GS Outcomes from Middle Outer Left) #
mol <- round(prop.table(mol) * 100, digits = 2)

# convert to data frame #
mol <- as.data.frame.matrix(mol)

# list of goal shot outcomes #
q <- c("Goal", "Saved", "Smothered", "Turnover")

# check to see if all data frames contain all columns, if not add column=0 #
mol[q[!(q %in% colnames(mol))]] = 0

# rename all columns with prefix MOL # 
colnames(mol) <- paste("MOL", colnames(mol), sep = "_")

return(mol)
} else {(mol <- setNames(as.data.frame(matrix(c(0, 0, 0, 0), ncol = 4, nrow = 1)), c("MOL_Goal", "MOL_Saved", "MOL_Smothered", "MOL_Turnover"))) 
  return(mol)}
}

mol_list <- list()
for (i in seq_along(goal_shots_list)){
 mol_list[[i]] <- mol_f(goal_shots_list[[i]])
}
Create table to identify goal shot outcomes from far outer right and convert to percentages
# Goal Shots Outcomes Far Outer Right #
forr_f <- function(x){
   if (any(grepl("Far_Outer_Right", x$GS.Location))){
# extract from Goal_Shots data frame only rows referring to Far Outer Right and save as separate data frame #
forr <- filter(x, GS.Location == "Far_Outer_Right")

# create table with Teams as rows and Goal Shot Outcomes as columns #
forr <- table(forr$Team, forr$Outcome)

# transform to percentages (% GS Outcomes from Far Outer Right) #
forr <- round(prop.table(forr) * 100, digits = 2)

# convert to data frame #
forr <- as.data.frame.matrix(forr)

# list of goal shot outcomes #
q <- c("Goal", "Saved", "Smothered", "Turnover")

# check to see if all data forrames contain all columns, if not add column=0 #
forr[q[!(q %in% colnames(forr))]] = 0

# rename all columns with prefix FOR #
colnames(forr) <- paste("FOR", colnames(forr), sep = "_")

return(forr)
} else {(forr <- setNames(as.data.frame(matrix(c(0, 0, 0, 0), ncol = 4, nrow = 1)), c("FOR_Goal", "FOR_Saved", "FOR_Smothered", "FOR_Turnover"))) 
  return(forr)}
}

forr_list <- list()
for (i in seq_along(goal_shots_list)){
 forr_list[[i]] <- forr_f(goal_shots_list[[i]])
}
Create table to identify goal shot outcomes from far inner right and convert to percentages
# Goal Shots Outcomes Far Inner Right #
fir_f <- function(x){
   if (any(grepl("Far_Inner_Right", x$GS.Location))){
# extract from Goal_Shots data frame only rows referring to Far Inner Right and save as separate data frame #
fir <- filter(x, GS.Location == "Far_Inner_Right")

# create table with Teams as rows and Goal Shot Outcomes as columns #
fir <- table(fir$Team, fir$Outcome)

# transform to percentages (% GS Outcomes from Far Inner Right) #
fir <- round(prop.table(fir) * 100, digits = 2)

# convert to data frame #
fir <- as.data.frame.matrix(fir)

# list of goal shot outcomes #
q <- c("Goal", "Saved", "Smothered", "Turnover")

# check to see if all data frames contain all columns, if not add column=0 #
fir[q[!(q %in% colnames(fir))]] = 0

# rename all columns with prefix FIR #
colnames(fir) <- paste("FIR", colnames(fir), sep = "_")

return(fir)
} else {(fir <- setNames(as.data.frame(matrix(c(0, 0, 0, 0), ncol = 4, nrow = 1)), c("FIR_Goal", "FIR_Saved", "FIR_Smothered", "FIR_Turnover"))) 
  return(fir)}
}

fir_list <- list()
for (i in seq_along(goal_shots_list)){
 fir_list[[i]] <- fir_f(goal_shots_list[[i]])
}
Create table to identify goal shot outcomes from far center and convert to percentages
# Goal Shots Outcomes Far Center #
fc_f <- function(x){
   if (any(grepl("Far_Center", x$GS.Location))){
# extract from Goal_Shots data frame only rows referring to Far Center and save as separate data frame #
fc <- filter(x, GS.Location == "Far_Center")

# create table with Teams as rows and Goal Shot Outcomes as columns #
fc <- table(fc$Team, fc$Outcome)

# transform to percentages (% GS Outcomes from Far Center) #
fc <- round(prop.table(fc) * 100, digits = 2)

# convert to data frame #
fc <- as.data.frame.matrix(fc)

# list of goal shot outcomes #
q <- c("Goal", "Saved", "Smothered", "Turnover")

# check to see if all data frames contain all columns, if not add column=0 #
fc[q[!(q %in% colnames(fc))]] = 0

# rename all columns with prefix FC #
colnames(fc) <- paste("FC", colnames(fc), sep = "_")

return(fc)
} else {(fc <- setNames(as.data.frame(matrix(c(0, 0, 0, 0), ncol = 4, nrow = 1)), c("FC_Goal", "FC_Saved", "FC_Smothered", "FC_Turnover"))) 
  return(fc)}
}

fc_list <- list()
for (i in seq_along(goal_shots_list)){
 fc_list[[i]] <- fc_f(goal_shots_list[[i]])
}
Create table to identify goal shot outcomes from far inner left and convert to percentages
# Goal Shots Outcomes Far Inner Left #
fil_f <- function(x){
   if (any(grepl("Far_Inner_Left", x$GS.Location))){
# extract from Goal_Shots data frame only rows referring to Far Inner Left and save as separate data frame #
fil <- filter(x, GS.Location == "Far_Inner_Left")

# create table with Teams as rows and Goal Shot Outcomes as columns #
fil <- table(fil$Team, fil$Outcome)

# transform to percentages (% GS Outcomes from Far Inner Left) #
fil <- round(prop.table(fil) * 100, digits = 2)

# convert to data frame #
fil <- as.data.frame.matrix(fil)

# list of goal shot outcomes #
q <- c("Goal", "Saved", "Smothered", "Turnover")

# check to see if all data frames contain all columns, if not add column=0 #
fil[q[!(q %in% colnames(fil))]] = 0

# rename all columns with prefix FIL #
colnames(fil) <- paste("FIL", colnames(fil), sep = "_")

return(fil)
} else {(fil <- setNames(as.data.frame(matrix(c(0, 0, 0, 0), ncol = 4, nrow = 1)), c("FIL_Goal", "FIL_Saved", "FIL_Smothered", "FIL_Turnover"))) 
  return(fil)}
}

fil_list <- list()
for (i in seq_along(goal_shots_list)){
 fil_list[[i]] <- fil_f(goal_shots_list[[i]])
}
Create table to identify goal shot outcomes from far outer left and convert to percentages
# Goal Shots Outcomes Far Outer Left #
fol_f <- function(x){
   if (any(grepl("Far_Outer_Left", x$GS.Location))){
# extract from Goal_Shots data frame only rows referring to Far Outer Left and save as separate data frame #
fol <- filter(x, GS.Location == "Far_Outer_Left")

# create table with Teams as rows and Goal Shot Outcomes as columns #
fol <- table(fol$Team, fol$Outcome)

# transform to percentages (% GS Outcomes from Far Outer Left) #
fol <- round(prop.table(fol) * 100, digits = 2)

# convert to data frame #
fol <- as.data.frame.matrix(fol)

# list of goal shot outcomes #
q <- c("Goal", "Saved", "Smothered", "Turnover")

# check to see if all data frames contain all columns, if not add column=0 #
fol[q[!(q %in% colnames(fol))]] = 0

# rename all columns with prefix FOL #
colnames(fol) <- paste("FOL", colnames(fol), sep = "_")

return(fol)
} else {(fol <- setNames(as.data.frame(matrix(c(0, 0, 0, 0), ncol = 4, nrow = 1)), c("FOL_Goal", "FOL_Saved", "FOL_Smothered", "FOL_Turnover"))) 
  return(fol)}
}

fol_list <- list()
for (i in seq_along(goal_shots_list)){
 fol_list[[i]] <- fol_f(goal_shots_list[[i]])
}
Join all teams goal shot outcomes into one
# join goal shot outcomes into one #
out_loc_list <- list()
for (i in seq_along(goal_shots_list)){
out_loc_list[[i]] <- bind_cols(gl_list[[i]], col_list[[i]], cil_list[[i]], cc_list[[i]], cir_list[[i]], cor_list[[i]], mol_list[[i]], mil_list[[i]], mc_list[[i]], mir_list[[i]], mor_list[[i]], fol_list[[i]], fil_list[[i]], fc_list[[i]], fir_list[[i]], forr_list[[i]])
}

# join teams goal shot outcomes into one #
out_loc <- bind_rows(out_loc_list)

# join teams goal shot locations into one #
loc <- bind_rows(loc_list)

# join locations and outcomes #
gs <- bind_cols(loc, out_loc)

#calculate z scores for all numeric variables
out_loc_z <- out_loc %>% mutate(across(where(is.numeric), scale))
loc_z <- loc %>% mutate(across(where(is.numeric), scale))

15. CALCULATE GOAL SHOT EFFICIENCY PER LOCATION

Calcultae efficiency by mulitplying shots/total by goals/shots
# calculate goal shot efficiency #
gs <- mutate(gs, GL_Eff = (Goal_Line * GL_Goal)/100, 
                 COL_Eff = (Close_Outer_Left * COL_Goal)/100, 
                 CIL_Eff = (Close_Inner_Left * CIL_Goal)/100, 
                 CC_Eff = (Close_Center * CC_Goal)/100, 
                 CIR_Eff = (Close_Inner_Right * CIR_Goal)/100, 
                 COR_Eff = (Close_Outer_Right * CIR_Goal)/100, 
                 MOL_Eff = (Middle_Outer_Left * MOL_Goal)/100, 
                 MIL_Eff = (Middle_Inner_Left * MIL_Goal)/100, 
                 MC_Eff = (Middle_Center * MC_Goal)/100, 
                 MIR_Eff = (Middle_Inner_Right * MIR_Goal)/100, 
                 MOR_Eff = (Middle_Outer_Right * MOR_Goal)/100, 
                 FOL_Eff = (Far_Outer_Left * FOL_Goal)/100, 
                 FIL_Eff = (Far_Inner_Left * FIL_Goal)/100, 
                 FC_Eff = (Far_Center * FC_Goal)/100, 
                 FIR_Eff = (Far_Inner_Right * FIR_Goal)/100, 
                 FOR_Eff = (Far_Outer_Right * FOR_Goal)/100)

16. EXPORT DATA

Tidy goal shot locations for presentation
# transform from wide to long #
loc_long <- loc %>%
  pivot_longer(cols = Goal_Line:Middle_Center,
               names_to = "Location",
               values_to = "Percent")

# change locations to factors and revalue #
loc_long$Location <- as.factor(loc_long$Location)
loc_long$Location <- revalue(loc_long$Location, c("Goal_Line" = "3_0.15", 
                                                  "Close_Outer_Left" = "3.65_0.15" , 
                                                  "Close_Inner_Left" = "3.4_0.4", 
                                                  "Close_Center" = "3_0.5", 
                                                  "Close_Inner_Right" = "2.6_0.4", 
                                                  "Close_Outer_Right" = "2.35_0.15", 
                                                  "Middle_Outer_Left" = "4.3_0.4", 
                                                  "Middle_Inner_Left" = "3.75_0.9", 
                                                  "Middle_Center" = "3_1.15", 
                                                  "Middle_Inner_Right" = "2.25_0.9", 
                                                  "Middle_Outer_Right" = "1.7_0.4", 
                                                  "Far_Outer_Left" = "5.2_0.75", 
                                                  "Far_Inner_Left" = "4.3_1.5", 
                                                  "Far_Center" = "3_1.8", 
                                                  "Far_Inner_Right" = "1.7_1.5", 
                                                  "Far_Outer_Right" = "0.8_0.75"))

# separate goal shot locations into side and depth #
loc_long <- separate(loc_long, Location, c("Loc.x", "Loc.y"), sep = "_")

# transform zscores from wide to long #
loc_z_long<-loc_z %>%
  pivot_longer(cols = Goal_Line:Middle_Center,
               names_to = "Location",
               values_to = "zscore")

# join zscores to real values #
loc_long <- bind_cols(loc_long, zscore = loc_z_long$zscore)

# convert to numeric #
loc_long$Loc.x <- as.numeric(loc_long$Loc.x)
loc_long$Loc.y <- as.numeric(loc_long$Loc.y)
loc_long$Percent <- as.numeric(loc_long$Percent)


# save data frame #
write_xlsx(loc_long, "Data/Processed Data//GS_Number_Women.xlsx")

loc_long2 <- head(loc_long)
knitr::kable(loc_long2, caption = "Team Goal Shot Locations Averages")
Team Goal Shot Locations Averages
Team Loc.x Loc.y Percent zscore
Argentina 3.00 0.15 2.24 -0.5799229
Argentina 3.75 0.90 6.72 -0.1284892
Argentina 4.30 0.40 10.45 2.1082650
Argentina 0.80 0.75 2.99 0.5232009
Argentina 1.70 1.50 11.94 0.8037715
Argentina 3.00 1.80 14.93 0.1484407
Tidy goal shot outcomes for presentation
# add team names #
out_loc <- rownames_to_column(out_loc, "Team")

# transform from wide to long #
out_loc_long <- out_loc %>%
  pivot_longer(cols = GL_Goal:FOR_Smothered,
               names_to = "Location",
               values_to = "Percent")

# separate descriptive column #
out_loc_long <- separate(out_loc_long, Location, c("Location", "Outcome"))

# change locations to factors and revalue #
out_loc_long$Location <- as.factor(out_loc_long$Location)
out_loc_long$Location <- revalue(out_loc_long$Location, c("GL" = "3_0.15",
                                                          "COL" = "3.65_0.15" , 
                                                          "CIL" = "3.4_0.4", 
                                                          "CC" = "3_0.5", 
                                                          "CIR" = "2.6_0.4", 
                                                          "COR" = "2.35_0.15", 
                                                          "MOL" = "4.3_0.4", 
                                                          "MIL" = "3.75_0.9", 
                                                          "MC" = "3_1.15", 
                                                          "MIR" = "2.25_0.9", 
                                                          "MOR" = "1.7_0.4", 
                                                          "FOL" = "5.2_0.75", 
                                                          "FIL" = "4.3_1.5", 
                                                          "FC" = "3_1.8", 
                                                          "FIR" = "1.7_1.5", 
                                                          "FOR" = "0.8_0.75"))

# separate goal shot locations into side and depth #
out_loc_long <- separate(out_loc_long, Location, c("Loc.x", "Loc.y"), sep = "_")

# transform zscores from wide to long #
out_loc_z_long <- out_loc_z %>%
  pivot_longer(cols = GL_Goal:FOR_Smothered,
               names_to = "Location",
               values_to = "zscore")

# join zscores to real values #
out_loc_long <- bind_cols(out_loc_long, zscore = out_loc_z_long$zscore)

# convert to numeric #
out_loc_long$Loc.x <- as.numeric(out_loc_long$Loc.x)
out_loc_long$Loc.y <- as.numeric(out_loc_long$Loc.y)
out_loc_long$Percent <- as.numeric(out_loc_long$Percent)


# save data frame #
write_xlsx(out_loc_long, "Data/Processed Data//GS_Outcomes_Women.xlsx")

out_loc_long2 <- head(out_loc_long)
knitr::kable(out_loc_long2, caption = "Team Goal Shot Outcomes Averages")
Team Goal Shot Outcomes Averages
Team Loc.x Loc.y Outcome Percent zscore
Argentina 3.00 0.15 Goal 66.67 -0.8519219
Argentina 3.00 0.15 Turnover 33.33 1.9653436
Argentina 3.00 0.15 Saved 0.00 -0.4714281
Argentina 3.00 0.15 Smothered 0.00 -0.3333333
Argentina 3.65 0.15 Goal 50.00 0.3698343
Argentina 3.65 0.15 Saved 50.00 1.4492462
Tidy goal shot efficiencies for presentation
# select columns for analysis #
eff <- select(gs, Team, GL_Eff, COL_Eff, CIL_Eff, CC_Eff, CIR_Eff, COR_Eff, MOL_Eff, MIL_Eff, MC_Eff, MIR_Eff, MOR_Eff, FOL_Eff, FIL_Eff, FC_Eff, FIR_Eff, FOR_Eff)

# calculate z scores for all numeric variables #
eff_z <- eff %>% mutate(across(where(is.numeric),scale))

# transform from wide to long #
eff_long <- eff %>%
  pivot_longer(cols = GL_Eff:FOR_Eff,
               names_to = "Location",
               values_to = "Efficiency")

# separate descriptive column #
eff_long <- separate(eff_long, Location, c("Location", "Outcome"))

# change locations to factors and revalue #
eff_long$Location <- as.factor(eff_long$Location)
eff_long$Location <- revalue(eff_long$Location, c("GL" = "3_0.15", 
                                                  "COL" = "3.65_0.15", 
                                                  "CIL" = "3.4_0.4", 
                                                  "CC" = "3_0.5", 
                                                  "CIR" = "2.6_0.4", 
                                                  "COR" = "2.35_0.15", 
                                                  "MOL" = "4.3_0.4", 
                                                  "MIL" = "3.75_0.9", 
                                                  "MC" = "3_1.15", 
                                                  "MIR" = "2.25_0.9", 
                                                  "MOR" = "1.7_0.4", 
                                                  "FOL" = "5.2_0.75", 
                                                  "FIL" = "4.3_1.5", 
                                                  "FC" = "3_1.8", 
                                                  "FIR" = "1.7_1.5", 
                                                  "FOR" = "0.8_0.75"))

# separate goal shot locations into side and depth #
eff_long <- separate(eff_long, Location, c("Loc.x", "Loc.y"), sep = "_")

# transform zscores from wide to long #
eff_z_long <- eff_z %>%
  pivot_longer(cols = GL_Eff:FOR_Eff,
               names_to = "Location",
               values_to = "zscore")

# join scores to real values #
eff_long <- bind_cols(eff_long, zscore = eff_z_long$zscore)

# convert to numeric #
eff_long$Loc.x <- as.numeric(eff_long$Loc.x)
eff_long$Loc.y <- as.numeric(eff_long$Loc.y)
eff_long$Efficiency <- as.numeric(eff_long$Efficiency)


# save data frame #
write_xlsx(eff_long, "Data/Processed Data//GS_Efficiency_Women.xlsx")

eff_long2 <- head(eff_long)
knitr::kable(eff_long2, caption = "Team Goal Shot Efficiency Averages")
Team Goal Shot Efficiency Averages
Team Loc.x Loc.y Outcome Efficiency zscore
Argentina 3.00 0.15 Eff 1.493408 -0.7215586
Argentina 3.65 0.15 Eff 0.745000 -0.1703123
Argentina 3.40 0.40 Eff 2.982708 0.8501426
Argentina 3.00 0.50 Eff 2.238867 -0.9694350
Argentina 2.60 0.40 Eff 1.493184 -0.1355461
Argentina 2.35 0.15 Eff 1.243209 1.5820048

PENALTY CORNER PATTERNS

17. FILTER

Extract set pieces rows
sp_f <- function(x){
# extract from all data (Game) rows referring to Set Pieces and save as separate data frame #
sp <- filter(x, B == "Set")

return(sp)
}

sp_list <- list()
for (i in seq_along(game_list)){
  sp_list[[i]] <- sp_f(game_list[[i]])
}
Count total set pieces per team
t_f <- function(x){
# count number of set pieces per game and save as separate data frame #
t <- as.data.frame(nrow(x)) %>% rename("T" = "nrow(x)")

return(t)
}

t_list <- list()
for (i in seq_along(sp_list)){
  t_list[[i]] <- t_f(sp_list[[i]])
}

18. CALCULATE STRAIGHT SHOT ATTEMPTS AND OUTCOMES

Extract straight shots from set pieces attempts
ss_f <- function(x){
# extract from set piece rows referring straight shot pattern and save as separate data frame #
ss <- filter(x, descriptors... == "Pass" & ...7 == "Maintain" & ...8 == "Goal Shot")

return(ss)
}

ss_list <- list()
for (i in seq_along(sp_list)){
  ss_list[[i]] <- ss_f(sp_list[[i]])
}
Count number of straight shot set pieces per team
ss_shots_f <- function(x){
# count number of straight shots and save as separate data frame #
ss_shots <- as.data.frame(nrow(x)) %>% rename("SS_Shots" = "nrow(x)")

return(ss_shots)
}

ss_shots_list <- list()
for (i in seq_along(ss_list)){
  ss_shots_list[[i]] <- ss_shots_f(ss_list[[i]])
}
Extract goals from straight shot set pieces
ss_goal_f <- function(x){
# extract from straight shot rows referring to goals and save as separate data frame #
ss_goal <- filter(x,  ...11 == "Goal")

ss_goal <- as.data.frame(nrow(ss_goal)) %>% rename("SS_Goal" = "nrow(ss_goal)")

return(ss_goal)
}

ss_goal_list <- list()
for (i in seq_along(ss_list)){
  ss_goal_list[[i]] <- ss_goal_f(ss_list[[i]])
}
Create table to identify goal shot outcomes from straight shot set pieces
ss_outcome_f <- function(x){
  if(nrow(x)>0){
# create table with Teams as rows and Goal Shot Outcomes as columns #
ss_outcome <- table(x$Team, x$...11)

# convert to data frame #
ss_outcome <- as.data.frame.matrix(ss_outcome)

# list of goal shot outcomes #
q <- c("Goal", "Saved", "Smothered", "Turnover")

# check to see if all data frames contain all columns, if not add column=0 #
ss_outcome[q[!(q %in% colnames(ss_outcome))]] = 0

# rename all columns with prefix SS #
colnames(ss_outcome) <- paste("SS", colnames(ss_outcome), sep = "_")

return(ss_outcome)
  } else {(ss_outcome <- setNames(as.data.frame(matrix(c(0, 0, 0, 0), ncol = 4, nrow = 1)), c("SS_Goal", "SS_Saved", "SS_Smothered", "SS_Turnover"))) 
  return(ss_outcome)}
}

ss_outcome_list <- list()
for (i in seq_along(ss_list)){
  ss_outcome_list[[i]] <- ss_outcome_f(ss_list[[i]])
}

19. CALCULATE LAYOFF ATTEMPTS AND OUTCOMES

Extract layoffs from set pieces attempts
l_f <- function(x){
# extract from set piece rows referring layoff pattern and save as separate data frame #
l <- filter(x, descriptors... == "Pass" & ...7 == "Maintain" & ...8 == "Pass" & ...9 == "Maintain" & ...10 == "Goal Shot")

return(l)
}

l_list <- list()
for (i in seq_along(sp_list)){
  l_list[[i]] <- l_f(sp_list[[i]])
}
Count number of layoff set pieces per team
l_shots_f <- function(x){
# count number of layoffs and save as separate data frame #
l_shots <- as.data.frame(nrow(x)) %>% rename("L_Shots" = "nrow(x)")

return(l_shots)
}

l_shots_list <- list()
for (i in seq_along(l_list)){
  l_shots_list[[i]] <- l_shots_f(l_list[[i]])
}
Extract goals from layoff set pieces
l_goal_f <- function(x){
# extract from layoff rows referring to goals and save as separate data frame #
l_goal <- filter(x, ...13 == "Goal")

l_goal <- as.data.frame(nrow(l_goal)) %>% rename("L_Goal" = "nrow(l_goal)")

return(l_goal)
}

l_goal_list <- list()
for (i in seq_along(l_list)){
  l_goal_list[[i]] <- l_goal_f(l_list[[i]])
}
Create table to identify goal shot outcomes from layoff set pieces
l_outcome_f <- function(x){
  if(nrow(x)>0){
# create table with Teams as rows and Goal Shot Outcomes as columns #
l_outcome <- table(x$Team, x$...13)

# convert to data frame #
l_outcome <- as.data.frame.matrix(l_outcome)

# list of goal shot outcomes #
q <- c("Goal", "Saved", "Smothered", "Turnover")

# check to see if all data frames contain all columns, if not add column=0 #
l_outcome[q[!(q %in% colnames(l_outcome))]] = 0

# rename all columns with prefix L #
colnames(l_outcome) <- paste("L", colnames(l_outcome), sep = "_")

return(l_outcome)
  } else {(l_outcome <- setNames(as.data.frame(matrix(c(0, 0, 0, 0), ncol = 4, nrow = 1)), c("L_Goal", "L_Saved", "L_Smothered", "L_Turnover"))) 
  return(l_outcome)}
}

l_outcome_list <- list()
for (i in seq_along(l_list)){
  l_outcome_list[[i]] <- l_outcome_f(l_list[[i]])
}

20. CALCULATE DRIBBLE AND SHOOT ATTEMPTS AND OUTCOMES

Extract dribbles from set pieces attempts
d_f <- function(x){
# extract from set piece rows referring dribble pattern and save as separate data frame #
d <- filter(x, descriptors... == "Pass" & ...7 == "Maintain" & ...8 == "Dribble" & ...10 == "Goal Shot")

return(d)
}

d_list <- list()
for (i in seq_along(sp_list)){
  d_list[[i]] <- d_f(sp_list[[i]])
}
Count number of dribble set pieces per team
d_shots_f <- function(x){
# count number of dribbles and save as separate data frame #
d_shots <- as.data.frame(nrow(x)) %>% rename("D_Shots" = "nrow(x)")

return(d_shots)
}

d_shots_list <- list()
for (i in seq_along(d_list)){
  d_shots_list[[i]] <- d_shots_f(d_list[[i]])
}
Extract goals from dribble set pieces
d_goal_f <- function(x){
# extract from dribble rows referring to goals and save as separate data frame #
d_goal <- filter(x,  ...13 == "Goal")

d_goal <- as.data.frame(nrow(d_goal)) %>% rename("D_Goal" = "nrow(d_goal)")

return(d_goal)
}

d_goal_list <- list()
for (i in seq_along(d_list)){
  d_goal_list[[i]] <- d_goal_f(d_list[[i]])
}
Create table to identify goal shot outcomes from dribble set pieces
d_outcome_f <- function(x){
  if(nrow(x)>0){
# create table with Teams as rows and Goal Shot Outcomes as columns #
d_outcome <- table(x$Team, x$...13)

#convert to data frame #
d_outcome <- as.data.frame.matrix(d_outcome)

# list of goal shot outcomes #
q <- c("Goal", "Saved", "Smothered", "Turnover")

# check to see if all data frames contain all columns, if not add column=0 #
d_outcome[q[!(q %in% colnames(d_outcome))]] = 0

# rename all columns with prefix D #
colnames(d_outcome) <- paste("D", colnames(d_outcome), sep = "_")

return(d_outcome)
  } else {(d_outcome <- setNames(as.data.frame(matrix(c(0, 0, 0, 0), ncol = 4, nrow = 1)), c("D_Goal", "D_Saved", "D_Smothered", "D_Turnover"))) 
  return(d_outcome)}
}

d_outcome_list <- list()
for (i in seq_along(d_list)){
  d_outcome_list[[i]] <- d_outcome_f(d_list[[i]])
}

21. CALCULATE DEFLECTION ATTEMPTS AND OUTCOMES

Extract deflections from set pieces attempts
def_f <- function(x){
# extract from set piece rows referring deflection pattern and save as separate data #
# frame #
def <- filter(x, descriptors... == "Pass" & ...7 == "Maintain" & ...8 == "Pass" & ...9 == "Gain" & ...10 == "Goal Shot")

return(def)
}

def_list <- list()
for (i in seq_along(sp_list)){
def_list[[i]] <- def_f(sp_list[[i]])
}
Count number of deflection set pieces per team
def_shots_f <- function(x){
# count number of deflections and save as separate data frame #
def_shots <- as.data.frame(nrow(x)) %>% rename("Def_Shots" = "nrow(x)")

return(def_shots)
}

def_shots_list <- list()
for (i in seq_along(def_list)){
def_shots_list[[i]] <- def_shots_f(def_list[[i]])
}
Extract goals from deflection set pieces
def_goal_f <- function(x){
# extract from deflection rows referring to goals and save as separate data frame #
def_goal <- filter(x,  ...13 == "Goal")

def_goal <- as.data.frame(nrow(def_goal)) %>% rename("Def_Goal" = "nrow(def_goal)")

return(def_goal)
}

def_goal_list <- list()
for (i in seq_along(def_list)){
def_goal_list[[i]] <- def_goal_f(def_list[[i]])
}
Create table to identify goal shot outcomes from deflection set pieces
def_outcome_f <- function(x){
  if(nrow(x)>0){
# create table with Teams as rows and Goal Shot Outcomes as columns #
def_outcome <- table(x$Team, x$...13)

# convert to data frame #
def_outcome <- as.data.frame.matrix(def_outcome)

# list of goal shot outcomes #
q <- c("Goal", "Saved", "Smothered", "Turnover")

# check to see if all data frames contain all columns, if not add column=0 #
def_outcome[q[!(q %in% colnames(def_outcome))]] = 0

# rename all columns with prefix Def #
colnames(def_outcome) <- paste("Def", colnames(def_outcome), sep = "_")

return(def_outcome)
  } else {(def_outcome <- setNames(as.data.frame(matrix(c(0, 0, 0, 0), ncol = 4, nrow = 1)), c("Def_Goal", "Def_Saved", "Def_Smothered", "Def_Turnover"))) 
  return(def_outcome)}
}

def_outcome_list <- list()
for (i in seq_along(def_list)){
  def_outcome_list[[i]] <- def_outcome_f(def_list[[i]])
}

22. CALCULATE ASSIST ATTEMPTS AND OUTCOMES

Extract assists from set pieces attempts
a_f <- function(x){
# extract from set piece rows referring assist pattern and save as separate data frame #
a <- filter(x, descriptors... == "Pass" & ...7 == "Maintain" & ...8 == "Pass" & ...9 == "Penetrating" & ...10 == "Goal Shot")

return(a)
}

a_list <- list()
for (i in seq_along(sp_list)){
  a_list[[i]] <- a_f(sp_list[[i]])
}
Count number of assist set pieces per team
a_shots_f <- function(x){
# count number of assists and save as separate data frame #
a_shots <- as.data.frame(nrow(x)) %>% rename("A_Shots" = "nrow(x)")

return(a_shots)
}

a_shots_list <- list()
for (i in seq_along(a_list)){
  a_shots_list[[i]] <- a_shots_f(a_list[[i]])
}
Extract goals from assist set pieces
a_goal_f <- function(x){
# extract from assist rows referring to goals and save as separate data frame #
a_goal <- filter(x,  ...13 == "Goal")

a_goal <- as.data.frame(nrow(a_goal)) %>% rename("A_Goal" = "nrow(a_goal)")

return(a_goal)
}

a_goal_list <- list()
for (i in seq_along(a_list)){
  a_goal_list[[i]] <- a_goal_f(a_list[[i]])
}
Create table to identify goal shot outcomes from assist set pieces
a_outcome_f <- function(x){
  if(nrow(x)>0){
# create table with Teams as rows and Goal Shot Outcomes as columns #
a_outcome <- table(x$Team, x$...13)

# convert to data frame #
a_outcome <- as.data.frame.matrix(a_outcome)

# list of goal shot outcomes #
q <- c("Goal", "Saved", "Smothered", "Turnover")

# check to see if all data frames contain all columns, if not add column=0 #
a_outcome[q[!(q %in% colnames(a_outcome))]] = 0

# rename all columns with prefix A #
colnames(a_outcome) <- paste("A", colnames(a_outcome), sep = "_")

return(a_outcome)
  } else {(a_outcome <- setNames(as.data.frame(matrix(c(0, 0, 0, 0), ncol = 4, nrow = 1)), c("A_Goal", "A_Saved", "A_Smothered", "A_Turnover"))) 
  return(a_outcome)}
}

a_outcome_list <- list()
for (i in seq_along(a_list)){
  a_outcome_list[[i]] <- a_outcome_f(a_list[[i]])
}

23. EXPORT DATA

Merge into 3 main patterns and convert to percentages
# join patterns into one #
patterns_list <- list()
for (i in seq_along(sp_list)){
  patterns_list[[i]] <- bind_cols(t_list[[i]], ss_shots_list[i], l_shots_list[[i]], d_shots_list[[i]], def_shots_list[[i]], a_shots_list[[i]], ss_outcome_list[i], l_outcome_list[[i]], d_outcome_list[[i]], def_outcome_list[[i]], a_outcome_list[[i]])
 
  # merge into 3 main patterns #
  patterns_list[[i]] <- mutate(patterns_list[[i]], LD_Shots = (L_Shots + D_Shots), 
                                                   DefA_Shots = (Def_Shots + A_Shots), 
                                                   LD_Goal = (L_Goal + D_Goal), 
                                                   DefA_Goal = (Def_Goal + A_Goal), 
                                                   LD_Saved = (L_Saved + D_Saved), 
                                                   DefA_Saved = (Def_Saved + A_Saved), 
                                                   LD_Smothered = (L_Smothered + D_Smothered), 
                                                   DefA_Smothered = (Def_Smothered + A_Smothered), 
                                                   LD_Turnover = (L_Turnover + D_Turnover), 
                                                   DefA_Turnover = (Def_Turnover + A_Turnover))
  
  # convert to percentages #
  patterns_list[[i]] <- mutate(patterns_list[[i]], SS_P = (SS_Shots/T)*100, 
                                                   LD_P = (LD_Shots/T)*100,  
                                                   DefA_P = (DefA_Shots/T)*100, 
                                                   SS_G = (SS_Goal/SS_Shots)*100, 
                                                   LD_G = (LD_Goal/LD_Shots)*100, 
                                                   DefA_G = (DefA_Goal/DefA_Shots)*100, 
                                                   SS_Sa = (SS_Saved/SS_Shots)*100, 
                                                   LD_Sa = (LD_Saved/LD_Shots)*100, 
                                                   DefA_Sa = (DefA_Saved/DefA_Shots)*100, 
                                                   SS_Sm = (SS_Smothered/SS_Shots)*100, 
                                                   LD_Sm = (LD_Smothered/LD_Shots)*100, 
                                                   DefA_Sm = (DefA_Smothered/DefA_Shots)*100, 
                                                   SS_T = (SS_Turnover/SS_Shots)*100, 
                                                   LD_T = (LD_Turnover/LD_Shots)*100, 
                                                   DefA_T = (DefA_Turnover/DefA_Shots)*100)
}

# add game names #
game_names <- names(game_list)
for (i in seq_along(game_list)){
  patterns_list[[i]]$Team <- game_names[i]
}

# join all teams into one #
patterns <- bind_rows(patterns_list)
Tidy set piece patterns for presentation
# select columns for analysis
patterns_long <- select(patterns, SS_P:Team)

# transform from wide to long #
patterns_long <- patterns_long %>%
  pivot_longer(cols = SS_P:DefA_T,
               names_to = "Outcome",
               values_to = "Percent")

# separate descriptive column #
patterns_long <- separate(patterns_long, Outcome, c("Pattern", "Outcome"))

# convert outcome to factor and reorder #
patterns_long$Outcome <- factor(patterns_long$Outcome, levels = c("P", "G", "Sa", "Sm", "T"))


# save data frame #
write_xlsx(patterns_long, "Data/Processed Data//SP_Patterns_Women.xlsx")

patterns_long2 <- head(patterns_long)
knitr::kable(patterns_long2, caption = "Team Penalty Corner Patterns Averages")
Team Penalty Corner Patterns Averages
Team Pattern Outcome Percent
Argentina SS P 65.671642
Argentina LD P 17.910448
Argentina DefA P 5.970149
Argentina SS G 13.636364
Argentina LD G 0.000000
Argentina DefA G 25.000000

POSSESSIONS

24. FILTER WINNNG

Extract attack type rows when winning and calculate possession time, length and rate
poss_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
poss <- filter(x, A == "Attack" & is.na(B))

# filter match status #
poss <- poss %>%
  filter_all(any_vars(str_detect(., pattern = "Winning")))

# rename columns #
poss <- plyr::rename(poss, c("start time" = "start_time", 
                             "end time" = "end_time", 
                             "# descriptors" = "number"))
                     
# convert start and end time columns to time class #
poss$start_time <- as.POSIXct(poss$start_time, format = "%H:%M:%S")
poss$end_time <- as.POSIXct(poss$end_time, format = "%H:%M:%S")

# add new column from time difference #
poss <- mutate(poss, Time = (end_time - start_time))

# transform time to numeric #
poss$Time <- as.numeric(poss$Time)

# transform number to numeric #
poss$number <- as.numeric(poss$number)

# add new column length; minus game details and divide by 2 to cancel effect columns #
poss <- mutate(poss, Length = ((number-8)/2))

# add new column rate #
poss <- mutate(poss, Rate  = (Time/Length))

# select columns for analysis #
poss <- select(poss, Team, Time, Length, Rate)

return(poss)
}

poss_list <- list()
for (i in seq_along(game_list)){
  poss_list[[i]] <- poss_f(game_list[[i]])
}

25. CALCULATE WINNING POSSESSION TIME, LENGTH AND RATE

Count number of possessions in each time interval and convert to percentages
# join teams into one #
poss <- bind_rows(poss_list)

# count number of possessions with specified time #
w_time_10 <- poss %>%
    group_by(Team) %>%
    tally(Time < 10, name = "W_Less_Ten" )

w_time_20 <- poss %>%
    group_by(Team) %>%
    tally(Time > 10 & Time < 20, name = "W_Ten_Twenty")

w_time_30 <- poss %>%
    group_by(Team) %>%
    tally(Time > 20 & Time < 30, name = "W_Twenty_Thirty")

w_time_40 <- poss %>%
    group_by(Team) %>%
    tally(Time > 30 & Time < 40, name = "W_Thirty_Forty")

w_time_50 <- poss %>%
    group_by(Team) %>%
    tally(Time > 40 & Time < 50, name = "W_Forty_Fifty")

w_time_60 <- poss %>%
    group_by(Team) %>%
    tally(Time > 50 & Time < 60, name = "W_Fifty_Sixty")

w_time_100 <- poss %>%
    group_by(Team) %>%
    tally(Time > 60 & Time < 100, name = "W_Sixty_Hundred")

w_time_101 <- poss %>%
    group_by(Team) %>%
    tally(Time > 100, name = "W_Greater_Hundred")

# join times into one #
w_time_all <- bind_cols(w_time_10, w_time_20[, 2], w_time_30[, 2], w_time_40[, 2], w_time_50[, 2], w_time_60[, 2], w_time_100[, 2], w_time_101[, 2])

# add total attacks #
w_time_all <- mutate(w_time_all, W_Total = (W_Less_Ten + W_Ten_Twenty + W_Twenty_Thirty + W_Thirty_Forty + W_Forty_Fifty + W_Fifty_Sixty + W_Sixty_Hundred + W_Greater_Hundred))

# convert to percentages #
w_time_all <- mutate(w_time_all, W_Less_Ten_P = (W_Less_Ten/W_Total)*100, 
                                 W_Ten_Twenty_P = (W_Ten_Twenty/W_Total)*100, 
                                 W_Twenty_Thirty_P = (W_Twenty_Thirty/W_Total)*100, 
                                 W_Thirty_Forty_P = (W_Thirty_Forty/W_Total)*100, 
                                 W_Forty_Fifty_P = (W_Forty_Fifty/W_Total)*100, 
                                 W_Fifty_Sixty_P = (W_Fifty_Sixty/W_Total)*100, 
                                 W_Sixty_Hundred_P =  (W_Sixty_Hundred/W_Total)*100, 
                                 W_Greater_Hundred_P = (W_Greater_Hundred/W_Total)*100)
Count number of possessions in each length interval and convert to percentages
# count number of possessions with specified length #
w_length_2 <- poss %>%
    group_by(Team) %>%
    tally(Length <= 2, name = "W_Less_Two")

w_length_5 <- poss %>%
    group_by(Team) %>%
    tally(Length > 2 & Length <= 5, name = "W_Three_Five")

w_length_9 <- poss %>%
    group_by(Team) %>%
    tally(Length > 5 & Length <= 9, name = "W_Six_Nine")

w_length_15 <- poss %>%
    group_by(Team) %>%
    tally(Length > 9 & Length <= 15, name = "W_Ten_Fifteen")

w_length_20 <- poss %>%
    group_by(Team) %>%
    tally(Length > 15 & Length <= 20, name = "W_Sixteen_Twenty")

w_length_30 <- poss %>%
    group_by(Team) %>%
    tally(Length > 20 & Length <= 30, name = "W_TwentyOne_Thirty")

w_length_31 <- poss %>%
    group_by(Team) %>%
    tally(Length > 30 , name = "W_Greater_Thirty")

# join lengths into one #
w_length_all <- bind_cols(w_length_2, w_length_5[, 2], w_length_9[, 2], w_length_15[, 2], w_length_20[, 2], w_length_30[, 2], w_length_31[, 2])

# add total attacks #
w_length_all <- mutate(w_length_all, W_Total = (W_Less_Two + W_Three_Five + W_Six_Nine + W_Ten_Fifteen + W_Sixteen_Twenty + W_TwentyOne_Thirty + W_Greater_Thirty))
                
# convert to percentages # 
 w_length_all <- mutate(w_length_all, W_Less_Two_P = (W_Less_Two/W_Total)*100, 
                                      W_Three_Five_P = (W_Three_Five/W_Total)*100, 
                                      W_Six_Nine_P = (W_Six_Nine/W_Total)*100, 
                                      W_Ten_Fifteen_P = (W_Ten_Fifteen/W_Total)*100, 
                                      W_Sixteen_Twenty_P = (W_Sixteen_Twenty/W_Total)*100, 
                                      W_TwentyOne_Thirty_P = (W_TwentyOne_Thirty/W_Total)*100, 
                                      W_Greater_Thirty_P = (W_Greater_Thirty/W_Total)*100)                  
Count number of possessions in each rate interval and convert to percentages
# count number of possessions with specified rate #
w_rate_2 <- poss %>%
    group_by(Team) %>%
    tally(Rate <= 2, name = "W_Less_Two")

w_rate_3 <- poss %>%
    group_by(Team) %>%
    tally(Rate > 2 & Rate < 3, name = "W_Two_Three")

w_rate_4 <- poss %>%
    group_by(Team) %>%
    tally(Rate > 3 & Rate < 4, name = "W_Three_Four")

w_rate_8 <- poss %>%
    group_by(Team) %>%
    tally(Rate > 4 & Rate < 8, name = "W_Five_Eight")

w_rate_12 <- poss %>%
    group_by(Team) %>%
    tally(Rate > 8 & Rate < 12, name = "W_Nine_Twelve")

w_rate_13 <- poss %>%
    group_by(Team) %>%
    tally(Rate > 12, name = "W_Greater_Twelve")

# join rates into one #
w_rate_all <- bind_cols(w_rate_2, w_rate_3[, 2], w_rate_4[, 2], w_rate_8[, 2], w_rate_12[, 2], w_rate_13[, 2])

# add total attacks #
w_rate_all <- mutate(w_rate_all, W_Total = (W_Less_Two + W_Two_Three + W_Three_Four + W_Five_Eight + W_Nine_Twelve + W_Greater_Twelve))

# convert to percentages #
w_rate_all <- mutate(w_rate_all, W_Less_Two_P = (W_Less_Two/W_Total)*100, 
                                 W_Two_Three_P = (W_Two_Three/W_Total)*100, 
                                 W_Three_Four_P = (W_Three_Four/W_Total)*100, 
                                 W_Five_Eight_P = (W_Five_Eight/W_Total)*100, 
                                 W_Nine_Twelve_P = (W_Nine_Twelve/W_Total)*100, 
                                 W_Greater_Twelve_P = (W_Greater_Twelve/W_Total)*100)

26. FILTER LOSING

Extract attack type rows when losing and calculate possession time, length and rate
poss_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
poss <- filter(x, A == "Attack" & is.na(B))

# filter match status #
poss <- poss %>%
  filter_all(any_vars(str_detect(., pattern = "Losing")))

# rename columns #
poss <- plyr::rename(poss, c("start time" = "start_time", 
                             "end time" = "end_time", 
                             "# descriptors" = "number"))
                     
# convert start and end time columns to time class #
poss$start_time <- as.POSIXct(poss$start_time, format = "%H:%M:%S")
poss$end_time <- as.POSIXct(poss$end_time, format = "%H:%M:%S")

# add new column from time difference #
poss <- mutate(poss, Time = (end_time - start_time))

# transform time to numeric #
poss$Time <- as.numeric(poss$Time)

# transform time to numeric #
poss$number <- as.numeric(poss$number)

# add new column length; minus game details and divide by 2 to cancel effect columns #
poss <- mutate(poss, Length = ((number-8)/2))

# add new column rate #
poss<- mutate(poss, Rate  = (Time/Length))

# select columns for analysis #
poss <- select(poss, Team, Time, Length, Rate)

return(poss)
}

poss_list <- list()
for (i in seq_along(game_list)){
  poss_list[[i]] <- poss_f(game_list[[i]])
}

27. CALCULATE LOSING POSSESSION TIME, LENGTH AND RATE

Count number of possessions in each time interval and convert to percentages
# join teams into one #
poss <- bind_rows(poss_list)

# count number of possessions with specified time #
l_time_10 <- poss %>%
    group_by(Team) %>%
    tally(Time < 10, name = "L_Less_Ten" )

l_time_20 <- poss %>%
    group_by(Team) %>%
    tally(Time > 10 & Time < 20, name = "L_Ten_Twenty")

l_time_30 <- poss %>%
    group_by(Team) %>%
    tally(Time > 20 & Time < 30, name = "L_Twenty_Thirty")

l_time_40 <- poss %>%
    group_by(Team) %>%
    tally(Time > 30 & Time < 40, name = "L_Thirty_Forty")

l_time_50 <- poss %>%
    group_by(Team) %>%
    tally(Time > 40 & Time < 50, name = "L_Forty_Fifty")

l_time_60 <- poss %>%
    group_by(Team) %>%
    tally(Time > 50 & Time < 60, name = "L_Fifty_Sixty")

l_time_100 <- poss %>%
    group_by(Team) %>%
    tally(Time > 60 & Time < 100, name = "L_Sixty_Hundred")

l_time_101 <- poss %>%
    group_by(Team) %>%
    tally(Time > 100, name = "L_Greater_Hundred")

# join times into one #
l_time_all <- bind_cols(l_time_10, l_time_20[, 2], l_time_30[, 2], l_time_40[, 2], l_time_50[, 2], l_time_60[, 2], l_time_100[, 2], l_time_101[, 2])

# add total attacks #
l_time_all <- mutate(l_time_all, L_Total = (L_Less_Ten + L_Ten_Twenty + L_Twenty_Thirty + L_Thirty_Forty + L_Forty_Fifty + L_Fifty_Sixty + L_Sixty_Hundred + L_Greater_Hundred))

# convert to percentages #
l_time_all <- mutate(l_time_all, L_Less_Ten_P = (L_Less_Ten/L_Total)*100, 
                                 L_Ten_Twenty_P = (L_Ten_Twenty/L_Total)*100, 
                                 L_Twenty_Thirty_P = (L_Twenty_Thirty/L_Total)*100, 
                                 L_Thirty_Forty_P =  (L_Thirty_Forty/L_Total)*100, 
                                 L_Forty_Fifty_P = (L_Forty_Fifty/L_Total)*100, 
                                 L_Fifty_Sixty_P = (L_Fifty_Sixty/L_Total)*100, 
                                 L_Sixty_Hundred_P = (L_Sixty_Hundred/L_Total)*100, 
                                 L_Greater_Hundred_P = (L_Greater_Hundred/L_Total)*100)
Count number of possessions in each length interval and convert to percentages
# count number of possessions with specified length #
l_length_2 <- poss %>%
    group_by(Team) %>%
    tally(Length <= 2, name = "L_Less_Two")

l_length_5 <- poss %>%
    group_by(Team) %>%
    tally(Length > 2 & Length <= 5, name = "L_Three_Five")

l_length_9 <- poss %>%
    group_by(Team) %>%
    tally(Length > 5 & Length <= 9, name = "L_Six_Nine")

l_length_15 <- poss %>%
    group_by(Team) %>%
    tally(Length > 9 & Length <= 15, name = "L_Ten_Fifteen")

l_length_20 <- poss %>%
    group_by(Team) %>%
    tally(Length > 15 & Length <= 20, name = "L_Sixteen_Twenty")

l_length_30 <- poss %>%
    group_by(Team) %>%
    tally(Length > 20 & Length <= 30, name = "L_TwentyOne_Thirty")

l_length_31 <- poss %>%
    group_by(Team) %>%
    tally(Length > 30 , name = "L_Greater_Thirty")

# join lengths into one #
l_length_all <- bind_cols(l_length_2, l_length_5[, 2], l_length_9[, 2], l_length_15[, 2], l_length_20[, 2], l_length_30[, 2], l_length_31[, 2])

# add total attacks #
l_length_all <- mutate(l_length_all, L_Total = (L_Less_Two + L_Three_Five + L_Six_Nine + L_Ten_Fifteen + L_Sixteen_Twenty + L_TwentyOne_Thirty + L_Greater_Thirty))
   
# convert to percentages #              
l_length_all <- mutate(l_length_all, L_Less_Two_P = (L_Less_Two/L_Total)*100, 
                                     L_Three_Five_P = (L_Three_Five/L_Total)*100, 
                                     L_Six_Nine_P = (L_Six_Nine/L_Total)*100, 
                                     L_Ten_Fifteen_P = (L_Ten_Fifteen/L_Total)*100, 
                                     L_Sixteen_Twenty_P = (L_Sixteen_Twenty/L_Total)*100, 
                                     L_TwentyOne_Thirty_P = (L_TwentyOne_Thirty/L_Total)*100, 
                                     L_Greater_Thirty_P = (L_Greater_Thirty/L_Total)*100 )                  
Count number of possessions in each rate interval and convert to percentages
# count number of possessions with specified rate #
l_rate_2 <- poss %>%
    group_by(Team) %>%
    tally(Rate <= 2, name = "L_Less_Two")

l_rate_3 <- poss %>%
    group_by(Team) %>%
    tally(Rate > 2 & Rate < 3, name = "L_Two_Three")

l_rate_4 <- poss %>%
    group_by(Team) %>%
    tally(Rate > 3 & Rate < 4, name = "L_Three_Four")

l_rate_8 <- poss %>%
    group_by(Team) %>%
    tally(Rate > 4 & Rate < 8, name = "L_Five_Eight")

l_rate_12 <- poss %>%
    group_by(Team) %>%
    tally(Rate > 8 & Rate < 12, name = "L_Nine_Twelve")

l_rate_13 <- poss %>%
    group_by(Team) %>%
    tally(Rate > 12, name = "L_Greater_Twelve")

# join rates into one #
l_rate_all <- bind_cols(l_rate_2, l_rate_3[, 2], l_rate_4[, 2], l_rate_8[, 2], l_rate_12[, 2], l_rate_13[, 2])

# add total attacks #
l_rate_all <- mutate(l_rate_all, L_Total = (L_Less_Two + L_Two_Three + L_Three_Four + L_Five_Eight + L_Nine_Twelve + L_Greater_Twelve))

# convert to percentages #
l_rate_all <- mutate(l_rate_all, L_Less_Two_P = (L_Less_Two/L_Total)*100, 
                                 L_Two_Three_P = (L_Two_Three/L_Total)*100, 
                                 L_Three_Four_P = (L_Three_Four/L_Total)*100, 
                                 L_Five_Eight_P = (L_Five_Eight/L_Total)*100, 
                                 L_Nine_Twelve_P = (L_Nine_Twelve/L_Total)*100, 
                                 L_Greater_Twelve_P = (L_Greater_Twelve/L_Total)*100)

28. FILTER DRAWING

Extract attack type rows when drawing and calculate possession time, length and rate
poss_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
poss <- filter(x, A == "Attack" & is.na(B))

# filter match status #
poss <- poss %>%
  filter_all(any_vars(str_detect(., pattern = "Drawing")))

# rename columns #
poss <- plyr::rename(poss, c("start time" = "start_time", 
                             "end time" = "end_time", 
                             "# descriptors" = "number"))
                     
# convert start and end time columns to time class #
poss$start_time <- as.POSIXct(poss$start_time, format = "%H:%M:%S")
poss$end_time <- as.POSIXct(poss$end_time, format = "%H:%M:%S")

# add new column from time difference #
poss <- mutate(poss, Time = (end_time - start_time))

# transform time to numeric #
poss$Time <- as.numeric(poss$Time)

# transform time to numeric #
poss$number <- as.numeric(poss$number)

#add new column length; minus game details and divide by 2 to cancel effect columns #
poss <- mutate(poss, Length = ((number-8)/2))

# add new column rate #
poss <- mutate(poss, Rate  = (Time/Length))

# select columns for analysis #
poss <- select(poss, Team, Time, Length, Rate)

return(poss)
}

poss_list <- list()
for (i in seq_along(game_list)){
  poss_list[[i]] <- poss_f(game_list[[i]])
}

29. CALCULATE DRAWING POSSESSION TIME, LENGTH AND RATE

Count number of possessions in each time interval and convert to percentages
# joins teams into one #
poss <- bind_rows(poss_list)

# count number of possessions for specified time #
d_time_10 <- poss %>%
    group_by(Team) %>%
    tally(Time < 10, name = "D_Less_Ten")

d_time_20 <- poss %>%
    group_by(Team) %>%
    tally(Time > 10 & Time < 20, name = "D_Ten_Twenty")

d_time_30 <- poss %>%
    group_by(Team) %>%
    tally(Time > 20 & Time < 30, name = "D_Twenty_Thirty")

d_time_40 <- poss %>%
    group_by(Team) %>%
    tally(Time > 30 & Time < 40, name = "D_Thirty_Forty")

d_time_50 <- poss %>%
    group_by(Team) %>%
    tally(Time > 40 & Time < 50, name = "D_Forty_Fifty")

d_time_60 <- poss %>%
    group_by(Team) %>%
    tally(Time > 50 & Time < 60, name = "D_Fifty_Sixty")

d_time_100 <- poss %>%
    group_by(Team) %>%
    tally(Time > 60 & Time < 100, name = "D_Sixty_Hundred")

d_time_101 <- poss %>%
    group_by(Team) %>%
    tally(Time > 100, name = "D_Greater_Hundred")

# join times into one #
d_time_all <- bind_cols(d_time_10, d_time_20[, 2], d_time_30[, 2], d_time_40[, 2], d_time_50[, 2], d_time_60[, 2], d_time_100[, 2], d_time_101[, 2])

# add total attacks #
d_time_all <- mutate(d_time_all, D_Total = (D_Less_Ten + D_Ten_Twenty + D_Twenty_Thirty + D_Thirty_Forty + D_Forty_Fifty + D_Fifty_Sixty + D_Sixty_Hundred + D_Greater_Hundred))

# convert to percentages #
d_time_all <- mutate(d_time_all, D_Less_Ten_P = (D_Less_Ten/D_Total)*100, 
                                 D_Ten_Twenty_P = (D_Ten_Twenty/D_Total)*100, 
                                 D_Twenty_Thirty_P = (D_Twenty_Thirty/D_Total)*100, 
                                 D_Thirty_Forty_P = (D_Thirty_Forty/D_Total)*100, 
                                 D_Forty_Fifty_P = (D_Forty_Fifty/D_Total)*100, 
                                 D_Fifty_Sixty_P = (D_Fifty_Sixty/D_Total)*100, 
                                 D_Sixty_Hundred_P = (D_Sixty_Hundred/D_Total)*100, 
                                 D_Greater_Hundred_P = (D_Greater_Hundred/D_Total)*100)
Count number of possessions in each length interval and convert to percentages
# count number of possessions with specified length #
d_length_2 <- poss %>%
    group_by(Team) %>%
    tally(Length <= 2, name = "D_Less_Two")

d_length_5 <- poss %>%
    group_by(Team) %>%
    tally(Length > 2 & Length <= 5, name = "D_Three_Five")

d_length_9 <- poss %>%
    group_by(Team) %>%
    tally(Length > 5 & Length <= 9, name = "D_Six_Nine")

d_length_15 <- poss %>%
    group_by(Team) %>%
    tally(Length > 9 & Length <= 15, name = "D_Ten_Fifteen")

d_length_20 <- poss %>%
    group_by(Team) %>%
    tally(Length > 15 & Length <= 20, name = "D_Sixteen_Twenty")

d_length_30 <- poss %>%
    group_by(Team) %>%
    tally(Length > 20 & Length <= 30, name = "D_TwentyOne_Thirty")

d_length_31 <- poss %>%
    group_by(Team) %>%
    tally(Length > 30 , name = "D_Greater_Thirty")

#join lengths into one#
d_length_all <- bind_cols(d_length_2, d_length_5[, 2], d_length_9[, 2], d_length_15[, 2], d_length_20[, 2], d_length_30[, 2], d_length_31[, 2])

# add total attacks #
d_length_all <- mutate(d_length_all, D_Total = (D_Less_Two + D_Three_Five + D_Six_Nine + D_Ten_Fifteen + D_Sixteen_Twenty + D_TwentyOne_Thirty + D_Greater_Thirty))
         
# convert to percentages #      
d_length_all <- mutate(d_length_all, D_Less_Two_P = (D_Less_Two/D_Total)*100, 
                                     D_Three_Five_P = (D_Three_Five/D_Total)*100, 
                                     D_Six_Nine_P = (D_Six_Nine/D_Total)*100, 
                                     D_Ten_Fifteen_P = (D_Ten_Fifteen/D_Total)*100, 
                                     D_Sixteen_Twenty_P = (D_Sixteen_Twenty/D_Total)*100, 
                                     D_TwentyOne_Thirty_P = (D_TwentyOne_Thirty/D_Total)*100, 
                                     D_Greater_Thirty_P = (D_Greater_Thirty/D_Total)*100)                  
Count number of possessions in each rate interval and convert to percentages
# count number of possessions with specified rate #
d_rate_2 <- poss %>%
    group_by(Team) %>%
    tally(Rate <= 2, name = "D_Less_Two")

d_rate_3 <- poss %>%
    group_by(Team) %>%
    tally(Rate > 2 & Rate < 3, name = "D_Two_Three")

d_rate_4 <- poss %>%
    group_by(Team) %>%
    tally(Rate > 3 & Rate < 4, name = "D_Three_Four")

d_rate_8 <- poss %>%
    group_by(Team) %>%
    tally(Rate > 4 & Rate < 8, name = "D_Five_Eight")

d_rate_12 <- poss %>%
    group_by(Team) %>%
    tally(Rate > 8 & Rate < 12, name = "D_Nine_Twelve")

d_rate_13 <- poss %>%
    group_by(Team) %>%
    tally(Rate > 12, name = "D_Greater_Twelve")

# join rates into one #
d_rate_all <- bind_cols(d_rate_2, d_rate_3[, 2], d_rate_4[, 2], d_rate_8[, 2], d_rate_12[, 2], d_rate_13[, 2])

# add total attacks #
d_rate_all <- mutate(d_rate_all, D_Total = (D_Less_Two + D_Two_Three + D_Three_Four + D_Five_Eight + D_Nine_Twelve + D_Greater_Twelve))

# convert to percentages #
d_rate_all <- mutate(d_rate_all, D_Less_Two_P = (D_Less_Two/D_Total)*100, 
                                 D_Two_Three_P = (D_Two_Three/D_Total)*100, 
                                 D_Three_Four_P = (D_Three_Four/D_Total)*100, 
                                 D_Five_Eight_P = (D_Five_Eight/D_Total)*100, 
                                 D_Nine_Twelve_P = (D_Nine_Twelve/D_Total)*100, 
                                 D_Greater_Twelve_P = (D_Greater_Twelve/D_Total)*100)

30. EXPORT DATA

Calculate the league average for all teams possession times per match status and tidy table for presentation
# select columns for analysis #
win_time <- select(w_time_all, Team, W_Less_Ten_P, W_Ten_Twenty_P, W_Twenty_Thirty_P, W_Thirty_Forty_P, W_Forty_Fifty_P, W_Fifty_Sixty_P, W_Sixty_Hundred_P, W_Greater_Hundred_P)

lose_time <- select(l_time_all, L_Less_Ten_P, L_Ten_Twenty_P, L_Twenty_Thirty_P, L_Thirty_Forty_P, L_Forty_Fifty_P, L_Fifty_Sixty_P, L_Sixty_Hundred_P, L_Greater_Hundred_P)

draw_time <- select(d_time_all, D_Less_Ten_P, D_Ten_Twenty_P, D_Twenty_Thirty_P, D_Thirty_Forty_P, D_Forty_Fifty_P, D_Fifty_Sixty_P, D_Sixty_Hundred_P, D_Greater_Hundred_P)

# join match status into one #
time_all_long <- bind_cols(win_time, lose_time, draw_time)

# calculate average per variable #
time_avg <- data.frame(Percent = colMeans(time_all_long[, c(-1)]))

# add variable names #
time_avg <- rownames_to_column(time_avg, var = "Time")
time_avg <- separate(time_avg, Time, c("Match.Status", "Time", "x"))
time_avg <- unite(time_avg, Time, c("Time", "x"))

# save data frame #
write_xlsx(time_avg, "Data/Processed Data//Time_Avg_Women.xlsx")

time_avg2 <- head(time_avg)
knitr::kable(time_avg2, caption = "League Possession Time Averages")
League Possession Time Averages
Match.Status Time Percent
W Less_Ten 45.938438
W Ten_Twenty 22.863828
W Twenty_Thirty 12.662020
W Thirty_Forty 6.219470
W Forty_Fifty 4.159899
W Fifty_Sixty 2.373543
Calculate team averages for all possession times per match status and tidy table for presentation
# transform from wide to long #
time_all_long <- time_all_long %>%
    pivot_longer(cols = W_Less_Ten_P:D_Greater_Hundred_P,
                 names_to = "Time",
                 values_to = "Percent")

# separate descriptive column #
time_all_long <- separate(time_all_long, Time, c("Match.Status", "Time", "x"))
time_all_long <- unite(time_all_long, Time, c("Time", "x"))
 
# save data frame #
write_xlsx(time_all_long, "Data/Processed Data//Time_Women.xlsx")

time_all_long2 <- head(time_all_long)
knitr::kable(time_all_long2, caption = "Team Possession Time Averages")
Team Possession Time Averages
Team Match.Status Time Percent
Argentina W Less_Ten 41.308411
Argentina W Ten_Twenty 24.859813
Argentina W Twenty_Thirty 15.514019
Argentina W Thirty_Forty 6.915888
Argentina W Forty_Fifty 3.177570
Argentina W Fifty_Sixty 2.056075
Calculate the league average for all teams possession lengths per match status and tidy table for presentation
# select columns for analysis #
win_length <- select(w_length_all, Team, W_Less_Two_P, W_Three_Five_P, W_Six_Nine_P, W_Ten_Fifteen_P, W_Sixteen_Twenty_P, W_TwentyOne_Thirty_P, W_Greater_Thirty_P)

lose_length <- select(l_length_all, L_Less_Two_P, L_Three_Five_P, L_Six_Nine_P, L_Ten_Fifteen_P, L_Sixteen_Twenty_P, L_TwentyOne_Thirty_P, L_Greater_Thirty_P)

draw_length <- select(d_length_all, D_Less_Two_P, D_Three_Five_P, D_Six_Nine_P, D_Ten_Fifteen_P, D_Sixteen_Twenty_P, D_TwentyOne_Thirty_P, D_Greater_Thirty_P)

# join match status into one #
length_all_long <- bind_cols(win_length, lose_length, draw_length)

# calculate average per variable #
length_avg <- data.frame(Percent = colMeans(length_all_long[, c(-1)]))

# add variable names #
length_avg <- rownames_to_column(length_avg, var = "Length")
length_avg <- separate(length_avg, Length, c("Match.Status", "Length", "x"))
length_avg <- unite(length_avg, Length, c("Length", "x"))

# save data frame #
write_xlsx(length_avg, "Data/Processed Data//Length_Avg_Women.xlsx")

length_avg2 <- head(length_avg)
knitr::kable(length_avg2, caption = "League Possession Length Averages")
League Possession Length Averages
Match.Status Length Percent
W Less_Two 27.559269
W Three_Five 32.983695
W Six_Nine 21.627763
W Ten_Fifteen 11.909809
W Sixteen_Twenty 3.662754
W TwentyOne_Thirty 1.957098
Calculate team averages for all possession lengths per match status and tidy table for presentation
# transform from wide to long #
length_all_long <- length_all_long %>%
    pivot_longer(cols = W_Less_Two_P:D_Greater_Thirty_P,
                 names_to = "Length",
                 values_to = "Percent")

# separate descriptive column #
length_all_long <- separate(length_all_long, Length, c("Match.Status","Length", "x"))
length_all_long <- unite(length_all_long, Length, c("Length", "x"))

# save data frame #
write_xlsx(length_all_long, "Data/Processed Data//Length_Women.xlsx")

length_all_long2 <- head(length_all_long)
knitr::kable(length_all_long2, caption = "Team Possession Length Averages")
Team Possession Length Averages
Team Match.Status Length Percent
Argentina W Less_Two 24.268503
Argentina W Three_Five 35.111876
Argentina W Six_Nine 22.547332
Argentina W Ten_Fifteen 11.359725
Argentina W Sixteen_Twenty 3.442341
Argentina W TwentyOne_Thirty 2.237522
Calculate the league average for all teams possession rates per match status and tidy table for presentation
# select columns for analysis #
win_rate <- select(w_rate_all, Team, W_Less_Two_P, W_Two_Three_P, W_Three_Four_P, W_Five_Eight_P, W_Nine_Twelve_P, W_Greater_Twelve_P)

lose_rate <- select(l_rate_all, L_Less_Two_P, L_Two_Three_P, L_Three_Four_P, L_Five_Eight_P, L_Nine_Twelve_P, L_Greater_Twelve_P)

draw_rate <- select(d_rate_all, D_Less_Two_P, D_Two_Three_P, D_Three_Four_P, D_Five_Eight_P, D_Nine_Twelve_P, D_Greater_Twelve_P)

# join match status into one #
rate_all_long <- bind_cols(win_rate, lose_rate, draw_rate)

# calculate average per variable #
rate_avg <- data.frame(Percent = colMeans(rate_all_long[, c(-1)]))

# add variable names #
rate_avg <- rownames_to_column(rate_avg, var = "Rate")
rate_avg <- separate(rate_avg, Rate, c("Match.Status", "Rate", "x"))
rate_avg <- unite(rate_avg, Rate, c("Rate", "x"))

# save data frame #
write_xlsx(rate_avg, "Data/Processed Data//Rate_Avg_Women.xlsx")

rate_avg2 <- head(rate_avg)
knitr::kable(rate_avg2, caption = "League Possession Rate Averages")
League Possession Rate Averages
Match.Status Rate Percent
W Less_Two 27.3334349
W Two_Three 33.6893243
W Three_Four 22.0221253
W Five_Eight 15.5342407
W Nine_Twelve 0.6568001
W Greater_Twelve 0.7640748
Calculate team averages for all teams possession rates per match status and tidy table for presentation
# transform from wide to long #
rate_all_long <- rate_all_long %>%
    pivot_longer(cols = W_Less_Two_P:D_Greater_Twelve_P,
                 names_to = "Rate",
                 values_to = "Percent")

# separate descriptive column #
rate_all_long <- separate(rate_all_long, Rate, c("Match.Status", "Rate", "x"))
rate_all_long <- unite(rate_all_long, Rate, c("Rate", "x"))

# save data frame #
write_xlsx(rate_all_long, "Data/Processed Data//Rate_Women.xlsx")

rate_all_long2 <- head(rate_all_long)
knitr::kable(rate_all_long2, caption = "Team Possession Rate Averages")
Team Possession Rate Averages
Team Match.Status Rate Percent
Argentina W Less_Two 20.6967213
Argentina W Two_Three 34.0163934
Argentina W Three_Four 28.0737705
Argentina W Five_Eight 15.5737705
Argentina W Nine_Twelve 0.8196721
Argentina W Greater_Twelve 0.8196721

START LOCATIONS

31. CALCULATE WINNING POSSESSION START LOCATIONS

Count possessions with start location in D25 when winning
# D25 #
start_D25_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_D25 <- filter(x, A == "Attack" & is.na(B))
  
# filter location #
start_D25 <- start_D25 %>%
  filter_all(any_vars(str_detect(., pattern = "D25 Att")))

# filter match status #
start_D25 <- start_D25 %>%
  filter_all(any_vars(str_detect(., pattern = "Winning")))

# create data frame #
start_D25 <- as.data.frame(nrow(start_D25))

return(start_D25)
}

start_D25_list <- list()
for (i in seq_along(game_list)){
  start_D25_list[[i]] <- start_D25_f(game_list[[i]])
}
Count possessions with start location in D50 when winning
# D50 #
start_D50_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_D50 <- filter(x, A == "Attack" & is.na(B))
  
# filter location #
start_D50 <- start_D50 %>%
  filter_all(any_vars(str_detect(., pattern = "D50 Att")))

# filter match status #
start_D50 <- start_D50 %>%
  filter_all(any_vars(str_detect(., pattern = "Winning")))

# create data frame #
start_D50 <- as.data.frame(nrow(start_D50))

return(start_D50)
}

start_D50_list <- list()
for (i in seq_along(game_list)){
  start_D50_list[[i]] <- start_D50_f(game_list[[i]])
}
Count possessions with start location in A50 when winning
# A50 #
start_A50_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_A50 <- filter(x, A == "Attack" & is.na(B))
  
# filter location #
start_A50 <- start_A50 %>%
  filter_all(any_vars(str_detect(., pattern = "A50 Att")))

# filter match status #
start_A50 <- start_A50 %>%
  filter_all(any_vars(str_detect(., pattern = "Winning")))

# create data frame #
start_A50 <- as.data.frame(nrow(start_A50))

return(start_A50)
}

start_A50_list <- list()
for (i in seq_along(game_list)){
  start_A50_list[[i]] <- start_A50_f(game_list[[i]])
}
Count possessions with start location in A25 when winning
# A25 #
start_A25_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_A25 <- filter(x, A == "Attack" & is.na(B))
  
# filter location #
start_A25 <- start_A25 %>%
  filter_all(any_vars(str_detect(., pattern = "A25 Att")))

# filter match status #
start_A25 <- start_A25 %>%
  filter_all(any_vars(str_detect(., pattern = "Winning")))

# create data frame #
start_A25 <- as.data.frame(nrow(start_A25))

return(start_A25)
}

start_A25_list <- list()
for (i in seq_along(game_list)){
  start_A25_list[[i]] <- start_A25_f(game_list[[i]])
}
Count possessions with start location in AC when winning
# AC #
start_AC_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_AC <- filter(x, A == "Attack" & is.na(B))
 
# filter location #
start_AC <- start_AC %>%
  filter_all(any_vars(str_detect(., pattern = "AC Att")))

# filter match status #
start_AC <- start_AC %>%
  filter_all(any_vars(str_detect(., pattern = "Winning")))

# create data frame #
start_AC <- as.data.frame(nrow(start_AC))


return(start_AC)
}

start_AC_list <- list()
for (i in seq_along(game_list)){
  start_AC_list[[i]] <- start_AC_f(game_list[[i]])
}
Join start locations into one to convert to percentages
# make one data frame out of lists #
d25 <- bind_rows(start_D25_list)
d50 <- bind_rows(start_D50_list)
a50 <- bind_rows(start_A50_list)
a25 <- bind_rows(start_A25_list)
ac <- bind_rows(start_AC_list)

# join locations into one and rename columns #
w_start <- bind_cols(ac, a25, a50, d50, d25) %>%
  rename("AC" = "nrow(start_AC)", 
         "A25" = "nrow(start_A25)", 
         "A50" = "nrow(start_A50)",
         "D50" = "nrow(start_D50)", 
         "D25" = "nrow(start_D25)")

# add total attacks #
w_start <- mutate(w_start, Total = (AC + A25 + A50 + D50 + D25))

# convert to percentages #
w_start <- mutate(w_start, W_AC_P = (AC/Total)*100, 
                           W_A25_P = (A25/Total)*100, 
                           W_A50_P = (A50/Total)*100, 
                           W_D50_P = (D50/Total)*100, 
                           W_D25_P = (D25/Total)*100)

# add names #
game_names <- names(game_list)
w_start$Team <- (game_names)

32. CALCULATE LOSING POSSESSION START LOCATIONS

Count possessions with start location in D25 when losing
# D25 #
start_D25_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_D25 <- filter(x, A == "Attack" & is.na(B))
  
# filter location #
start_D25 <- start_D25 %>%
  filter_all(any_vars(str_detect(., pattern = "D25 Att")))

# filter match status #
start_D25 <- start_D25 %>%
  filter_all(any_vars(str_detect(., pattern = "Losing")))

# create data frame #
start_D25 <- as.data.frame(nrow(start_D25))

return(start_D25)
}

start_D25_list <- list()
for (i in seq_along(game_list)){
  start_D25_list[[i]] <- start_D25_f(game_list[[i]])
}
Count possessions with start location in D50 when losing
# D50 #
start_D50_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_D50 <- filter(x, A == "Attack" & is.na(B))
  
# filter location #
start_D50 <- start_D50 %>%
  filter_all(any_vars(str_detect(., pattern = "D50 Att")))

# filter match status #
start_D50 <- start_D50 %>%
  filter_all(any_vars(str_detect(., pattern = "Losing")))

# create data frame #
start_D50 <- as.data.frame(nrow(start_D50))

return(start_D50)
}

start_D50_list <- list()
for (i in seq_along(game_list)){
  start_D50_list[[i]] <- start_D50_f(game_list[[i]])
}
Count possessions with start location in A50 when losing
# A50 #
start_A50_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_A50 <- filter(x, A == "Attack" & is.na(B))
  
# filter location #
start_A50 <- start_A50 %>%
  filter_all(any_vars(str_detect(., pattern = "A50 Att")))

# filter match status #
start_A50 <- start_A50 %>%
  filter_all(any_vars(str_detect(., pattern = "Losing")))

# create data frame #
start_A50 <- as.data.frame(nrow(start_A50))

return(start_A50)
}

start_A50_list <- list()
for (i in seq_along(game_list)){
  start_A50_list[[i]] <- start_A50_f(game_list[[i]])
}
Count possessions with start location in A25 when losing
# A25 #
start_A25_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_A25 <- filter(x, A == "Attack" & is.na(B))
  
# filter location #
start_A25 <- start_A25 %>%
  filter_all(any_vars(str_detect(., pattern = "A25 Att")))

# filter match status #
start_A25 <- start_A25 %>%
  filter_all(any_vars(str_detect(., pattern = "Losing")))

# create data frame #
start_A25 <- as.data.frame(nrow(start_A25))

return(start_A25)
}

start_A25_list <- list()
for (i in seq_along(game_list)){
  start_A25_list[[i]] <- start_A25_f(game_list[[i]])
}
Count possessions with start location in AC when losing
# AC # 
start_AC_f <- function(x){
#extract from all data (Game) rows referring to Attack and save as separate data frame #
start_AC <- filter(x, A == "Attack" & is.na(B))
  
# filter location #
start_AC <- start_AC %>%
  filter_all(any_vars(str_detect(., pattern = "AC Att")))

# filter match status #
start_AC <- start_AC %>%
  filter_all(any_vars(str_detect(., pattern = "Losing")))

# create data frame #
start_AC <- as.data.frame(nrow(start_AC))

return(start_AC)
}

start_AC_list <- list()
for (i in seq_along(game_list)){
  start_AC_list[[i]] <- start_AC_f(game_list[[i]])
}
Join start locations into one to convert to percentages
# make one data frame out of lists #
d25 <- bind_rows(start_D25_list)
d50 <- bind_rows(start_D50_list)
a50 <- bind_rows(start_A50_list)
a25 <- bind_rows(start_A25_list)
ac <- bind_rows(start_AC_list)

# join locations into one and rename columns #
l_start <- bind_cols(ac, a25, a50, d50, d25) %>%
  rename("AC" = "nrow(start_AC)", 
         "A25" = "nrow(start_A25)", 
         "A50" = "nrow(start_A50)",
         "D50" = "nrow(start_D50)", 
         "D25" = "nrow(start_D25)")

# add total attacks #
l_start <- mutate(l_start, Total = (AC + A25 + A50 + D50 + D25))

# convert to percentages #
l_start <- mutate(w_start, L_AC_P = (AC/Total)*100, 
                           L_A25_P = (A25/Total)*100, 
                           L_A50_P = (A50/Total)*100, 
                           L_D50_P = (D50/Total)*100, 
                           L_D25_P = (D25/Total)*100)

33. CALCULATE DRAWING POSSESSION START LOCATIONS

Count possessions with start location in D25 when drawing
# D25 #
start_D25_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_D25 <- filter(x, A == "Attack" & is.na(B))
  
# filter location #
start_D25 <- start_D25 %>%
  filter_all(any_vars(str_detect(., pattern = "D25 Att")))

# filter match status #
start_D25 <- start_D25 %>%
  filter_all(any_vars(str_detect(., pattern = "Drawing")))

# create data frame #
start_D25 <- as.data.frame(nrow(start_D25))

return(start_D25)
}

start_D25_list <- list()
for (i in seq_along(game_list)){
  start_D25_list[[i]] <- start_D25_f(game_list[[i]])
}
Count possessions with start location in D50 when drawing
# D50 #
start_D50_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_D50 <- filter(x, A == "Attack" & is.na(B))
  
# filter location #
start_D50 <- start_D50 %>%
  filter_all(any_vars(str_detect(., pattern = "D50 Att")))

# filter match status #
start_D50 <- start_D50 %>%
  filter_all(any_vars(str_detect(., pattern = "Drawing")))

# create data frame #
start_D50 <- as.data.frame(nrow(start_D50))

return(start_D50)
}

start_D50_list <- list()
for (i in seq_along(game_list)){
  start_D50_list[[i]] <- start_D50_f(game_list[[i]])
}
Count possessions with start location in A50 when drawing
# A50 #
start_A50_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_A50 <- filter(x, A == "Attack" & is.na(B))
  
# filter location #
start_A50 <- start_A50 %>%
  filter_all(any_vars(str_detect(., pattern = "A50 Att")))

# filter match status #
start_A50 <- start_A50 %>%
  filter_all(any_vars(str_detect(., pattern = "Drawing")))

# create data frame #
start_A50 <- as.data.frame(nrow(start_A50))

return(start_A50)
}

start_A50_list <- list()
for (i in seq_along(game_list)){
  start_A50_list[[i]] <- start_A50_f(game_list[[i]])
}
Count possessions with start location in A25 when drawing
# A25 #
start_A25_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_A25 <- filter(x, A == "Attack" & is.na(B))
  
# filter location #
start_A25 <- start_A25 %>%
  filter_all(any_vars(str_detect(., pattern = "A25 Att")))

# filter match status #
start_A25 <- start_A25 %>%
  filter_all(any_vars(str_detect(., pattern = "Drawing")))

# create data frame #
start_A25 <- as.data.frame(nrow(start_A25))

return(start_A25)
}

start_A25_list <- list()
for (i in seq_along(game_list)){
  start_A25_list[[i]] <- start_A25_f(game_list[[i]])
}
Count possessions with start location in AC when drawing
# AC #
start_AC_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_AC <- filter(x, A == "Attack" & is.na(B))
  
# filter location #
start_AC <- start_AC %>%
  filter_all(any_vars(str_detect(., pattern = "AC Att")))

# filter match status #
start_AC <- start_AC %>%
  filter_all(any_vars(str_detect(., pattern = "Drawing")))

# create data frame #
start_AC <- as.data.frame(nrow(start_AC))

return(start_AC)
}

start_AC_list <- list()
for (i in seq_along(game_list)){
  start_AC_list[[i]] <- start_AC_f(game_list[[i]])
}
Join start locations into one to convert to percentages
# make one data frame out of lists #
d25 <- bind_rows(start_D25_list)
d50 <- bind_rows(start_D50_list)
a50 <- bind_rows(start_A50_list)
a25 <- bind_rows(start_A25_list)
ac <- bind_rows(start_AC_list)

# join locations into one and rename columns #
d_start <- bind_cols(ac, a25, a50, d50, d25) %>%
  rename("AC" = "nrow(start_AC)", 
         "A25" = "nrow(start_A25)", 
         "A50" = "nrow(start_A50)",
         "D50" = "nrow(start_D50)", 
         "D25" = "nrow(start_D25)")

# add total attacks #
d_start <- mutate(d_start, Total = (AC + A25 + A50 + D50 + D25))

# convert to percentages #
d_start <- mutate(d_start, D_AC_P = (AC/Total)*100, 
                           D_A25_P = (A25/Total)*100, 
                           D_A50_P = (A50/Total)*100, 
                           D_D50_P = (D50/Total)*100, 
                           D_D25_P = (D25/Total)*100)

34. EXPORT DATA

Calculate the league average for all teams start locations per match status and tidy table for presentation
# select columns for analysis #
win_start <- select(w_start, Team, W_AC_P, W_A25_P, W_A50_P, W_D50_P, W_D25_P)

lose_start <- select(l_start, L_AC_P, L_A25_P, L_A50_P, L_D50_P, L_D25_P)

draw_start <- select(d_start, D_AC_P, D_A25_P, D_A50_P, D_D50_P, D_D25_P)

# join match status into one #
start_long <- bind_cols(win_start, lose_start, draw_start)

# calculate average per variable #
start_avg <- data.frame(Percent = colMeans(start_long[, c(-1)]))

# add variable names #
start_avg <- rownames_to_column(start_avg, var = "Location")
start_avg <- separate(start_avg, Location, c("Match.Status", "Location"))

# save data frame #
write_xlsx(start_avg, "Data/Processed Data//Start_Avg_Women.xlsx")

start_avg2 <- head(start_avg)
knitr::kable(start_avg2, caption = "League Start Location Averages")
League Start Location Averages
Match.Status Location Percent
W AC 0.8256928
W A25 6.7049865
W A50 15.4491843
W D50 24.8877567
W D25 52.1323797
L AC 0.8256928
Calculate teams average for all start locations per match status and tidy table for presentation
# transform from wide to long #
start_long <- start_long %>%
  pivot_longer(cols = W_AC_P:D_D25_P,
               names_to = "Location",
               values_to = "Percent")

# separate descriptive column #
start_long <- separate(start_long, Location, c("Match.Status", "Location"))

# save data frame #
write_xlsx(start_long, "Data/Processed Data//Start_Women.xlsx")

start_long2 <- head(start_long)
knitr::kable(start_long2, caption = "Team Start Location Averages")
Team Start Location Averages
Team Match.Status Location Percent
Argentina W AC 0.172117
Argentina W A25 5.679862
Argentina W A50 20.137694
Argentina W D50 26.333907
Argentina W D25 47.676420
Argentina L AC 0.172117

START AND END LOCATIONS

35. CALCULATE D25 END LOCATIONS WHEN WINNING

Count possessions with start location in D25 and end location D25 when winning
# D25 D25 #
start_D25_D25_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_D25_D25 <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_D25_D25 <- start_D25_D25 %>%
  filter_all(any_vars(str_detect(., pattern = "D25 Att")))

# filter match status #
start_D25_D25 <- start_D25_D25 %>%
  filter_all(any_vars(str_detect(., pattern = "Winning")))

# filter end location #
start_D25_D25 <- start_D25_D25 %>%
  filter_all(any_vars(str_detect(., pattern = "D25")))

# create data frame #
start_D25_D25 <- as.data.frame(nrow(start_D25_D25))

return(start_D25_D25)
}

start_D25_D25_list <- list()
for (i in seq_along(game_list)){
  start_D25_D25_list[[i]] <- start_D25_D25_f(game_list[[i]])
}
Count possessions with start location in D25 and end location D50 when winning
# D25 D50 #
start_D25_D50_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_D25_D50 <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_D25_D50 <- start_D25_D50 %>%
  filter_all(any_vars(str_detect(., pattern = "D25 Att")))

# filter match status #
start_D25_D50 <- start_D25_D50 %>%
  filter_all(any_vars(str_detect(., pattern = "Winning")))

# filter end location #
start_D25_D50 <- start_D25_D50 %>%
  filter_all(any_vars(str_detect(., pattern = "D50")))

# create data frame #
start_D25_D50 <- as.data.frame(nrow(start_D25_D50))

return(start_D25_D50)
}

start_D25_D50_list <- list()
for (i in seq_along(game_list)){
  start_D25_D50_list[[i]] <- start_D25_D50_f(game_list[[i]])
}
Count possessions with start location in D25 and end location A50 when winning
# D25 A50 #
start_D25_A50_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_D25_A50 <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_D25_A50 <- start_D25_A50 %>%
  filter_all(any_vars(str_detect(., pattern = "D25 Att")))

# filter match status #
start_D25_A50 <- start_D25_A50 %>%
  filter_all(any_vars(str_detect(., pattern = "Winning")))

# filter end location #
start_D25_A50 <- start_D25_A50 %>%
  filter_all(any_vars(str_detect(., pattern = "A50")))

# create data frame #
start_D25_A50 <- as.data.frame(nrow(start_D25_A50))

return(start_D25_A50)
}

start_D25_A50_list <- list()
for (i in seq_along(game_list)){
  start_D25_A50_list[[i]] <- start_D25_A50_f(game_list[[i]])
}
Count possessions with start location in D25 and end location A25 when winning
# D25 A25 #
start_D25_A25_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_D25_A25 <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_D25_A25 <- start_D25_A25 %>%
  filter_all(any_vars(str_detect(., pattern = "D25 Att")))

# filter match status #
start_D25_A25 <- start_D25_A25 %>%
  filter_all(any_vars(str_detect(., pattern = "Winning")))

# filter end location #
start_D25_A25 <- start_D25_A25 %>%
  filter_all(any_vars(str_detect(., pattern = "A25")))

# create data frame #
start_D25_A25 <- as.data.frame(nrow(start_D25_A25))

return(start_D25_A25)
}

start_D25_A25_list <- list()
for (i in seq_along(game_list)){
  start_D25_A25_list[[i]] <- start_D25_A25_f(game_list[[i]])
}
Count possessions with start location in D25 and end location AC when winning
# D25 AC #
start_D25_AC_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_D25_AC <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_D25_AC <- start_D25_AC %>%
  filter_all(any_vars(str_detect(., pattern = "D25 Att")))

# filter match status #
start_D25_AC <- start_D25_AC %>%
  filter_all(any_vars(str_detect(., pattern = "Winning")))

# filter end location #
start_D25_AC <- start_D25_AC %>%
  filter_all(any_vars(str_detect(., pattern = "AC")))

# create data frame #
start_D25_AC <- as.data.frame(nrow(start_D25_AC))

return(start_D25_AC)
}

start_D25_AC_list <- list()
for (i in seq_along(game_list)){
  start_D25_AC_list[[i]] <- start_D25_AC_f(game_list[[i]])
}
Count possessions with start location in D25 and GS when winning
# D25 GS #
start_D25_GS_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_D25_GS <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_D25_GS <- start_D25_GS %>%
  filter_all(any_vars(str_detect(., pattern = "D25 Att")))

# filter match status #
start_D25_GS <- start_D25_GS %>%
  filter_all(any_vars(str_detect(., pattern = "Winning")))

# filter GS #
start_D25_GS <- start_D25_GS %>%
  filter_all(any_vars(str_detect(., pattern = "Goal Shot")))

# create data frame #
start_D25_GS <- as.data.frame(nrow(start_D25_GS))

return(start_D25_GS)
}

start_D25_GS_list <- list()
for (i in seq_along(game_list)){
  start_D25_GS_list[[i]] <- start_D25_GS_f(game_list[[i]])
}
Join end locations into one to convert to percentages
# make one data frame out of lists #
d25 <- bind_rows(start_D25_D25_list)
d50 <- bind_rows(start_D25_D50_list)
a50 <- bind_rows(start_D25_A50_list)
a25 <- bind_rows(start_D25_A25_list)
ac <- bind_rows(start_D25_AC_list)
gs <- bind_rows(start_D25_GS_list)

# join locations into one and rename columns #
w_d25_start <- bind_cols(gs, ac, a25, a50, d50, d25) %>%
  rename("GS" = "nrow(start_D25_GS)", 
         "AC" = "nrow(start_D25_AC)", 
         "A25" = "nrow(start_D25_A25)", 
         "A50" = "nrow(start_D25_A50)", 
         "D50" = "nrow(start_D25_D50)", 
         "D25" = "nrow(start_D25_D25)")

# add total attacks #
w_d25_start <- mutate(w_d25_start, Total = (GS + AC + A25 + A50 + D50 + D25))

# convert to percentages #
w_d25_start <- mutate(w_d25_start, W_D25_GS_P = (GS/Total)*100, 
                                   W_D25_AC_P = (AC/Total)*100, 
                                   W_D25_A25_P = (A25/Total)*100, 
                                   W_D25_A50_P = (A50/Total)*100, 
                                   W_D25_D50_P = (D50/Total)*100, 
                                   W_D25_D25_P = (D25/Total)*100)

36. CALCULATE D50 END LOCATIONS WHEN WINNING

Count possessions with start location in D50 and end location D25 when winning
# D50 D25 #
start_D50_D25_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_D50_D25 <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_D50_D25 <- start_D50_D25 %>%
  filter_all(any_vars(str_detect(., pattern = "D50 Att")))

# filter match status #
start_D50_D25 <- start_D50_D25 %>%
  filter_all(any_vars(str_detect(., pattern = "Winning")))

# filter end location #
start_D50_D25 <- start_D50_D25 %>%
  filter_all(any_vars(str_detect(., pattern = "D25")))

# create data frame #
start_D50_D25 <- as.data.frame(nrow(start_D50_D25))

return(start_D50_D25)
}

start_D50_D25_list <- list()
for (i in seq_along(game_list)){
  start_D50_D25_list[[i]] <- start_D50_D25_f(game_list[[i]])
}
Count possessions with start location in D50 and end location D50 when winning
# D50 D50 # 
start_D50_D50_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_D50_D50 <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_D50_D50 <- start_D50_D50 %>%
  filter_all(any_vars(str_detect(., pattern = "D50 Att")))

# filter match status #
start_D50_D50 <- start_D50_D50 %>%
  filter_all(any_vars(str_detect(., pattern = "Winning")))

# filter end location #
start_D50_D50 <- start_D50_D50 %>%
  filter_all(any_vars(str_detect(., pattern = "D50")))

# create data frame #
start_D50_D50 <- as.data.frame(nrow(start_D50_D50))

return(start_D50_D50)
}

start_D50_D50_list <- list()
for (i in seq_along(game_list)){
  start_D50_D50_list[[i]] <- start_D50_D50_f(game_list[[i]])
}
Count possessions with start location in D50 and end location A50 when winning
# D50 A50 #
start_D50_A50_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_D50_A50 <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_D50_A50 <- start_D50_A50 %>%
  filter_all(any_vars(str_detect(., pattern = "D50 Att")))

# filter match status #
start_D50_A50 <- start_D50_A50 %>%
  filter_all(any_vars(str_detect(., pattern = "Winning")))

# filter end location #
start_D50_A50 <- start_D50_A50 %>%
  filter_all(any_vars(str_detect(., pattern = "A50")))

# create data frame #
start_D50_A50 <- as.data.frame(nrow(start_D50_A50))

return(start_D50_A50)
}

start_D50_A50_list <- list()
for (i in seq_along(game_list)){
  start_D50_A50_list[[i]] <- start_D50_A50_f(game_list[[i]])
}
Count possessions with start location in D50 and end location A25 when winning
# D50 A25 # 
start_D50_A25_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_D50_A25 <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_D50_A25 <- start_D50_A25 %>%
  filter_all(any_vars(str_detect(., pattern = "D50 Att")))

# filter match status #
start_D50_A25 <- start_D50_A25 %>%
  filter_all(any_vars(str_detect(., pattern = "Winning")))

# filter end location #
start_D50_A25 <- start_D50_A25 %>%
  filter_all(any_vars(str_detect(., pattern = "A25")))

# create data frame #
start_D50_A25 <- as.data.frame(nrow(start_D50_A25))

return(start_D50_A25)
}

start_D50_A25_list <- list()
for (i in seq_along(game_list)){
  start_D50_A25_list[[i]] <- start_D50_A25_f(game_list[[i]])
}
Count possessions with start location in D50 and end location AC when winning
# D50 AC #
start_D50_AC_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_D50_AC <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_D50_AC <- start_D50_AC %>%
  filter_all(any_vars(str_detect(., pattern = "D50 Att")))

# filter match status #
start_D50_AC <- start_D50_AC %>%
  filter_all(any_vars(str_detect(., pattern = "Winning")))

# filter end location #
start_D50_AC <- start_D50_AC %>%
  filter_all(any_vars(str_detect(., pattern = "AC")))

# create data frame #
start_D50_AC <- as.data.frame(nrow(start_D50_AC))

return(start_D50_AC)
}

start_D50_AC_list <- list()
for (i in seq_along(game_list)){
  start_D50_AC_list[[i]] <- start_D50_AC_f(game_list[[i]])
}
Count possessions with start location in D50 and GS when winning
# D50 GS #
start_D50_GS_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_D50_GS <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_D50_GS <- start_D50_GS %>%
  filter_all(any_vars(str_detect(., pattern = "D50 Att")))

# filter match status #
start_D50_GS <- start_D50_GS %>%
  filter_all(any_vars(str_detect(., pattern = "Winning")))

# filter GS #
start_D50_GS <- start_D50_GS %>%
  filter_all(any_vars(str_detect(., pattern = "Goal Shot")))

# create data frame #
start_D50_GS <- as.data.frame(nrow(start_D50_GS))

return(start_D50_GS)
}

start_D50_GS_list <- list()
for (i in seq_along(game_list)){
  start_D50_GS_list[[i]] <- start_D50_GS_f(game_list[[i]])
}
Join end locations into one to convert to percentages
# make one data frame out of lists #
d25 <- bind_rows(start_D50_D25_list)
d50 <- bind_rows(start_D50_D50_list)
a50 <- bind_rows(start_D50_A50_list)
a25 <- bind_rows(start_D50_A25_list)
ac <- bind_rows(start_D50_AC_list)
gs <- bind_rows(start_D50_GS_list)

# join locations into one and rename columns #
w_d50_start <- bind_cols(gs, ac, a25, a50, d50, d25) %>%
  rename("GS" = "nrow(start_D50_GS)", 
         "AC" = "nrow(start_D50_AC)", 
         "A25" = "nrow(start_D50_A25)", 
         "A50" = "nrow(start_D50_A50)", 
         "D50" = "nrow(start_D50_D50)", 
         "D25" = "nrow(start_D50_D25)")

# add total attacks #
w_d50_start <- mutate(w_d50_start, Total = (GS + AC + A25 + A50 + D50 + D25))

# convert to percentages #
w_d50_start <- mutate(w_d50_start, W_D50_GS_P = (GS/Total)*100, 
                                   W_D50_AC_P = (AC/Total)*100, 
                                   W_D50_A25_P = (A25/Total)*100, 
                                   W_D50_A50_P = (A50/Total)*100, 
                                   W_D50_D50_P = (D50/Total)*100, 
                                   W_D50_D25_P = (D25/Total)*100)

37. CALCULATE A50 END LOCATIONS WHEN WINNING

Count possessions with start location in A50 and end location D25 when winning
# A50 D25 #
start_A50_D25_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_A50_D25 <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_A50_D25 <- start_A50_D25 %>%
  filter_all(any_vars(str_detect(., pattern = "A50 Att")))

# filter match status #
start_A50_D25 <- start_A50_D25 %>%
  filter_all(any_vars(str_detect(., pattern = "Winning")))

# filter end location #
start_A50_D25 <- start_A50_D25 %>%
  filter_all(any_vars(str_detect(., pattern = "D25")))

# create data frame #
start_A50_D25 <- as.data.frame(nrow(start_A50_D25))

return(start_A50_D25)
}

start_A50_D25_list <- list()
for (i in seq_along(game_list)){
  start_A50_D25_list[[i]] <- start_A50_D25_f(game_list[[i]])
}
Count possessions with start location in A50 and end location D50 when winning
# A50 D50 #
start_A50_D50_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_A50_D50 <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_A50_D50 <- start_A50_D50 %>%
  filter_all(any_vars(str_detect(., pattern = "A50 Att")))

# filter match status #
start_A50_D50 <- start_A50_D50 %>%
  filter_all(any_vars(str_detect(., pattern = "Winning")))

# filter end location #
start_A50_D50 <- start_A50_D50 %>%
  filter_all(any_vars(str_detect(., pattern = "D50")))

# create data frame #
start_A50_D50 <- as.data.frame(nrow(start_A50_D50))

return(start_A50_D50)
}

start_A50_D50_list <- list()
for (i in seq_along(game_list)){
  start_A50_D50_list[[i]] <- start_A50_D50_f(game_list[[i]])
}
Count possessions with start location in A50 and end location A50 when winning
# A50 A50 #
start_A50_A50_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_A50_A50 <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_A50_A50 <- start_A50_A50 %>%
  filter_all(any_vars(str_detect(., pattern = "A50 Att")))

# filter match status #
start_A50_A50 <- start_A50_A50 %>%
  filter_all(any_vars(str_detect(., pattern = "Winning")))

# filter end location #
start_A50_A50 <- start_A50_A50 %>%
  filter_all(any_vars(str_detect(., pattern = "A50")))

# create data frame #
start_A50_A50 <- as.data.frame(nrow(start_A50_A50))

return(start_A50_A50)
}

start_A50_A50_list <- list()
for (i in seq_along(game_list)){
  start_A50_A50_list[[i]] <- start_A50_A50_f(game_list[[i]])
}
Count possessions with start location in A50 and end location A25 when winning
# A50 A25 #
start_A50_A25_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_A50_A25 <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_A50_A25 <- start_A50_A25 %>%
  filter_all(any_vars(str_detect(., pattern = "A50 Att")))

# filter match status #
start_A50_A25 <- start_A50_A25 %>%
  filter_all(any_vars(str_detect(., pattern = "Winning")))

# filter end location #
start_A50_A25 <- start_A50_A25 %>%
  filter_all(any_vars(str_detect(., pattern = "A25")))

# create data frame #
start_A50_A25 <- as.data.frame(nrow(start_A50_A25))

return(start_A50_A25)
}

start_A50_A25_list <- list()
for (i in seq_along(game_list)){
  start_A50_A25_list[[i]] <- start_A50_A25_f(game_list[[i]])
}
Count possessions with start location in A50 and end location AC when winning
# A50 AC #
start_A50_AC_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_A50_AC <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_A50_AC <- start_A50_AC %>%
  filter_all(any_vars(str_detect(., pattern = "A50 Att")))

# filter match status #
start_A50_AC <- start_A50_AC %>%
  filter_all(any_vars(str_detect(., pattern = "Winning")))

# filter end location #
start_A50_AC <- start_A50_AC %>%
  filter_all(any_vars(str_detect(., pattern = "AC")))

# create data frame #
start_A50_AC <- as.data.frame(nrow(start_A50_AC))

return(start_A50_AC)
}

start_A50_AC_list <- list()
for (i in seq_along(game_list)){
  start_A50_AC_list[[i]] <- start_A50_AC_f(game_list[[i]])
}
Count possessions with start location in A50 and GS when winning
# A50 GS #
start_A50_GS_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_A50_GS <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_A50_GS <- start_A50_GS %>%
  filter_all(any_vars(str_detect(., pattern = "A50 Att")))

# filter match status #
start_A50_GS <- start_A50_GS %>%
  filter_all(any_vars(str_detect(., pattern = "Winning")))

# filter GS #
start_A50_GS <- start_A50_GS %>%
  filter_all(any_vars(str_detect(., pattern = "Goal Shot")))

# create data frame #
start_A50_GS <- as.data.frame(nrow(start_A50_GS))

return(start_A50_GS)
}

start_A50_GS_list <- list()
for (i in seq_along(game_list)){
  start_A50_GS_list[[i]] <- start_A50_GS_f(game_list[[i]])
}
Join end locations into one to convert to percentages
# make one data frame out of lists #
d25 <- bind_rows(start_A50_D25_list)
d50 <- bind_rows(start_A50_D50_list)
a50 <- bind_rows(start_A50_A50_list)
a25 <- bind_rows(start_A50_A25_list)
ac <- bind_rows(start_A50_AC_list)
gs <- bind_rows(start_A50_GS_list)

# join locations into one and rename columns #
w_a50_start <- bind_cols(gs, ac, a25, a50, d50, d25) %>%
  rename("GS" = "nrow(start_A50_GS)", 
         "AC" = "nrow(start_A50_AC)", 
         "A25" = "nrow(start_A50_A25)", 
         "A50" = "nrow(start_A50_A50)", 
         "D50" = "nrow(start_A50_D50)", 
         "D25" = "nrow(start_A50_D25)")

# add total attacks #
w_a50_start <- mutate(w_a50_start, Total = (GS + AC + A25 + A50 + D50 + D25))

# convert to percentages #
w_a50_start <- mutate(w_a50_start, W_A50_GS_P = (GS/Total)*100, 
                                   W_A50_AC_P = (AC/Total)*100, 
                                   W_A50_A25_P = (A25/Total)*100, 
                                   W_A50_A50_P = (A50/Total)*100, 
                                   W_A50_D50_P = (D50/Total)*100, 
                                   W_A50_D25_P = (D25/Total)*100)

38. CALCULATE A25 END LOCATIONS WHEN WINNING

Count possessions with start location in A25 and end location D25 when winning
# A25 D25 #
start_A25_D25_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_A25_D25 <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_A25_D25 <- start_A25_D25 %>%
  filter_all(any_vars(str_detect(., pattern = "A25 Att")))

# filter match status #
start_A25_D25 <- start_A25_D25 %>%
  filter_all(any_vars(str_detect(., pattern = "Winning")))

# filter end location #
start_A25_D25 <- start_A25_D25 %>%
  filter_all(any_vars(str_detect(., pattern = "D25")))

# create data frame #
start_A25_D25 <- as.data.frame(nrow(start_A25_D25))

return(start_A25_D25)
}

start_A25_D25_list <- list()
for (i in seq_along(game_list)){
  start_A25_D25_list[[i]] <- start_A25_D25_f(game_list[[i]])
}
Count possessions with start location in A25 and end location D50 when winning
# A25 D50 #
start_A25_D50_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_A25_D50 <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_A25_D50 <- start_A25_D50 %>%
  filter_all(any_vars(str_detect(., pattern = "A25 Att")))

# filter match status #
start_A25_D50 <- start_A25_D50 %>%
  filter_all(any_vars(str_detect(., pattern = "Winning")))

# filter end location #
start_A25_D50 <- start_A25_D50 %>%
  filter_all(any_vars(str_detect(., pattern = "D50")))

# create data frame #
start_A25_D50 <- as.data.frame(nrow(start_A25_D50))

return(start_A25_D50)
}

start_A25_D50_list <- list()
for (i in seq_along(game_list)){
  start_A25_D50_list[[i]] <- start_A25_D50_f(game_list[[i]])
}
Count possessions with start location in A25 and end location A50 when winning
# A25 A50 #
start_A25_A50_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_A25_A50 <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_A25_A50 <- start_A25_A50 %>%
  filter_all(any_vars(str_detect(., pattern = "A25 Att")))

# filter match status #
start_A25_A50 <- start_A25_A50 %>%
  filter_all(any_vars(str_detect(., pattern = "Winning")))

# filter end location #
start_A25_A50 <- start_A25_A50 %>%
  filter_all(any_vars(str_detect(., pattern = "A50")))

# create data frame #
start_A25_A50 <- as.data.frame(nrow(start_A25_A50))

return(start_A25_A50)
}

start_A25_A50_list <- list()
for (i in seq_along(game_list)){
  start_A25_A50_list[[i]] <- start_A25_A50_f(game_list[[i]])
}
Count possessions with start location in A25 and end location A25 when winning
# A25 A25 #
start_A25_A25_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_A25_A25 <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_A25_A25 <- start_A25_A25 %>%
  filter_all(any_vars(str_detect(., pattern = "A25 Att")))

# filter match status #
start_A25_A25 <- start_A25_A25 %>%
  filter_all(any_vars(str_detect(., pattern = "Winning")))

# filter end location #
start_A25_A25 <- start_A25_A25 %>%
  filter_all(any_vars(str_detect(., pattern = "A25")))

# create data frame #
start_A25_A25 <- as.data.frame(nrow(start_A25_A25))

return(start_A25_A25)
}

start_A25_A25_list <- list()
for (i in seq_along(game_list)){
  start_A25_A25_list[[i]] <- start_A25_A25_f(game_list[[i]])
}
Count possessions with start location in A25 and end location AC when winning
# A25 AC #
start_A25_AC_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_A25_AC <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_A25_AC <- start_A25_AC %>%
  filter_all(any_vars(str_detect(., pattern = "A25 Att")))

# filter match status #
start_A25_AC <- start_A25_AC %>%
  filter_all(any_vars(str_detect(., pattern = "Winning")))

# filter end location #
start_A25_AC <- start_A25_AC %>%
  filter_all(any_vars(str_detect(., pattern = "AC")))

# create data frame #
start_A25_AC <- as.data.frame(nrow(start_A25_AC))

return(start_A25_AC)
}

start_A25_AC_list <- list()
for (i in seq_along(game_list)){
  start_A25_AC_list[[i]] <- start_A25_AC_f(game_list[[i]])
}
Count possessions with start location in A25 and end location GS when winning
# A25 GS #
start_A25_GS_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_A25_GS <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_A25_GS <- start_A25_GS %>%
  filter_all(any_vars(str_detect(., pattern = "A25 Att")))

# filter match status #
start_A25_GS <- start_A25_GS %>%
  filter_all(any_vars(str_detect(., pattern = "Winning")))

# filter GS #
start_A25_GS <- start_A25_GS %>%
  filter_all(any_vars(str_detect(., pattern = "Goal Shot")))

# create data frame #
start_A25_GS <- as.data.frame(nrow(start_A25_GS))

return(start_A25_GS)
}

start_A25_GS_list <- list()
for (i in seq_along(game_list)){
  start_A25_GS_list[[i]] <- start_A25_GS_f(game_list[[i]])
}
Join end locations into one to convert to percentages
# make one data frame out of lists #
d25 <- bind_rows(start_A25_D25_list)
d50 <- bind_rows(start_A25_D50_list)
a50 <- bind_rows(start_A25_A50_list)
a25 <- bind_rows(start_A25_A25_list)
ac <- bind_rows(start_A25_AC_list)
gs <- bind_rows(start_A25_GS_list)

# join locations into one and rename columns #
w_a25_start <- bind_cols(gs, ac, a25, a50, d50, d25) %>%
  rename("GS" = "nrow(start_A25_GS)", 
         "AC" = "nrow(start_A25_AC)", 
         "A25" = "nrow(start_A25_A25)", 
         "A50" = "nrow(start_A25_A50)", 
         "D50" = "nrow(start_A25_D50)", 
         "D25" = "nrow(start_A25_D25)")

# add total attacks #
w_a25_start <- mutate(w_a25_start, Total = (GS + AC + A25 + A50 + D50 + D25))

# convert to percentages #
w_a25_start <- mutate(w_a25_start, W_A25_GS_P = (GS/Total)*100,
                                   W_A25_AC_P = (AC/Total)*100,
                                   W_A25_A25_P = (A25/Total)*100, 
                                   W_A25_A50_P = (A50/Total)*100, 
                                   W_A25_D50_P = (D50/Total)*100, 
                                   W_A25_D25_P = (D25/Total)*100)

# add names #
game_names <- names(game_list)
w_a25_start$Team <- game_names

39. CALCULATE D25 END LOCATIONS WHEN LOSING

Count possessions with start location in D25 and end location D25 when losing
# D25 D25 #
start_D25_D25_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_D25_D25 <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_D25_D25 <- start_D25_D25 %>%
  filter_all(any_vars(str_detect(., pattern = "D25 Att")))

# filter match status #
start_D25_D25 <- start_D25_D25 %>%
  filter_all(any_vars(str_detect(., pattern = "Losing")))

# filter end location #
start_D25_D25 <- start_D25_D25 %>%
  filter_all(any_vars(str_detect(., pattern = "D25")))

# create data frame #
start_D25_D25 <- as.data.frame(nrow(start_D25_D25))

return(start_D25_D25)
}

start_D25_D25_list <- list()
for (i in seq_along(game_list)){
  start_D25_D25_list[[i]] <- start_D25_D25_f(game_list[[i]])
}
Count possessions with start location in D25 and end location D50 when losing
# D25 D50 #
start_D25_D50_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_D25_D50 <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_D25_D50 <- start_D25_D50 %>%
  filter_all(any_vars(str_detect(., pattern = "D25 Att")))

# filter match status #
start_D25_D50 <- start_D25_D50 %>%
  filter_all(any_vars(str_detect(., pattern = "Losing")))

# filter end location #
start_D25_D50 <- start_D25_D50 %>%
  filter_all(any_vars(str_detect(., pattern = "D50")))

# create data frame #
start_D25_D50 <- as.data.frame(nrow(start_D25_D50))

return(start_D25_D50)
}

start_D25_D50_list <- list()
for (i in seq_along(game_list)){
  start_D25_D50_list[[i]] <- start_D25_D50_f(game_list[[i]])
}
Count possessions with start location in D25 and end location A50 when losing
# D25 A50 #
start_D25_A50_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_D25_A50 <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_D25_A50 <- start_D25_A50 %>%
  filter_all(any_vars(str_detect(., pattern = "D25 Att")))

# filter match status #
start_D25_A50 <- start_D25_A50 %>%
  filter_all(any_vars(str_detect(., pattern = "Losing")))

# filter end location #
start_D25_A50 <- start_D25_A50 %>%
  filter_all(any_vars(str_detect(., pattern = "A50")))

# create data frame #
start_D25_A50 <- as.data.frame(nrow(start_D25_A50))

return(start_D25_A50)
}

start_D25_A50_list <- list()
for (i in seq_along(game_list)){
  start_D25_A50_list[[i]] <- start_D25_A50_f(game_list[[i]])
}
Count possessions with start location in D25 and end location A25 when losing
# D25 A25 #
start_D25_A25_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_D25_A25 <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_D25_A25 <- start_D25_A25 %>%
  filter_all(any_vars(str_detect(., pattern = "D25 Att")))

# filter match status #
start_D25_A25 <- start_D25_A25 %>%
  filter_all(any_vars(str_detect(., pattern = "Losing")))

# filter end location #
start_D25_A25 <- start_D25_A25 %>%
  filter_all(any_vars(str_detect(., pattern = "A25")))

# create data frame #
start_D25_A25 <- as.data.frame(nrow(start_D25_A25))

return(start_D25_A25)
}

start_D25_A25_list <- list()
for (i in seq_along(game_list)){
  start_D25_A25_list[[i]] <- start_D25_A25_f(game_list[[i]])
}
Count possessions with start location in D25 and end location AC when losing
# D25 AC #
start_D25_AC_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_D25_AC <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_D25_AC <- start_D25_AC %>%
  filter_all(any_vars(str_detect(., pattern = "D25 Att")))

# filter match status #
start_D25_AC <- start_D25_AC %>%
  filter_all(any_vars(str_detect(., pattern = "Losing")))

# filter end location #
start_D25_AC <- start_D25_AC %>%
  filter_all(any_vars(str_detect(., pattern = "AC")))

# create data frame #
start_D25_AC <- as.data.frame(nrow(start_D25_AC))

return(start_D25_AC)
}

start_D25_AC_list <- list()
for (i in seq_along(game_list)){
  start_D25_AC_list[[i]] <- start_D25_AC_f(game_list[[i]])
}
Count possessions with start location in D25 and GS when losing
# D25 GS # 
start_D25_GS_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_D25_GS <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_D25_GS <- start_D25_GS %>%
  filter_all(any_vars(str_detect(., pattern = "D25 Att")))

# filter match status #
start_D25_GS <- start_D25_GS %>%
  filter_all(any_vars(str_detect(., pattern = "Losing")))

# filter GS #
start_D25_GS <- start_D25_GS %>%
  filter_all(any_vars(str_detect(., pattern = "Goal Shot")))

# create data frame #
start_D25_GS <- as.data.frame(nrow(start_D25_GS))

return(start_D25_GS)
}

start_D25_GS_list <- list()
for (i in seq_along(game_list)){
  start_D25_GS_list[[i]] <- start_D25_GS_f(game_list[[i]])
}
Join end locations into one to convert to percentages
# make one data frame out of lists #
d25 <- bind_rows(start_D25_D25_list)
d50 <- bind_rows(start_D25_D50_list)
a50 <- bind_rows(start_D25_A50_list)
a25 <- bind_rows(start_D25_A25_list)
ac <- bind_rows(start_D25_AC_list)
gs <- bind_rows(start_D25_GS_list)

# join locations into one and rename columns #
l_d25_start <- bind_cols(gs, ac, a25, a50, d50, d25) %>%
  rename("GS" = "nrow(start_D25_GS)", 
         "AC" = "nrow(start_D25_AC)", 
         "A25" = "nrow(start_D25_A25)", 
         "A50" = "nrow(start_D25_A50)", 
         "D50" = "nrow(start_D25_D50)", 
         "D25" = "nrow(start_D25_D25)")

# add total attacks #
l_d25_start <- mutate(l_d25_start, Total = (GS + AC + A25 + A50 + D50 + D25))

# convert to percentages #
l_d25_start <- mutate(l_d25_start, L_D25_GS_P = (GS/Total)*100, 
                                   L_D25_AC_P = (AC/Total)*100, 
                                   L_D25_A25_P = (A25/Total)*100, 
                                   L_D25_A50_P = (A50/Total)*100, 
                                   L_D25_D50_P = (D50/Total)*100, 
                                   L_D25_D25_P = (D25/Total)*100)

40. CALCULATE D50 END LOCATIONS WHEN LOSING

Count possessions with start location in D50 and end location D25 when losing
# D50 D25 #
start_D50_D25_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_D50_D25 <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_D50_D25 <- start_D50_D25 %>%
  filter_all(any_vars(str_detect(., pattern = "D50 Att")))

# filter match status #
start_D50_D25 <- start_D50_D25 %>%
  filter_all(any_vars(str_detect(., pattern = "Losing")))

# filter end location #
start_D50_D25 <- start_D50_D25 %>%
  filter_all(any_vars(str_detect(., pattern = "D25")))

# create data frame #
start_D50_D25 <- as.data.frame(nrow(start_D50_D25))

return(start_D50_D25)
}

start_D50_D25_list <- list()
for (i in seq_along(game_list)){
  start_D50_D25_list[[i]] <- start_D50_D25_f(game_list[[i]])
}
Count possessions with start location in D50 and end location D50 when losing
# D50 D50 #
start_D50_D50_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_D50_D50 <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_D50_D50 <- start_D50_D50 %>%
  filter_all(any_vars(str_detect(., pattern = "D50 Att")))

# filter match status #
start_D50_D50 <- start_D50_D50 %>%
  filter_all(any_vars(str_detect(., pattern = "Losing")))

# filter end location #
start_D50_D50 <- start_D50_D50 %>%
  filter_all(any_vars(str_detect(., pattern = "D50")))

# create data frame #
start_D50_D50 <- as.data.frame(nrow(start_D50_D50))

return(start_D50_D50)
}

start_D50_D50_list <- list()
for (i in seq_along(game_list)){
  start_D50_D50_list[[i]] <- start_D50_D50_f(game_list[[i]])
}
Count possessions with start location in D50 and end location A50 when losing
# D50 A50 #
start_D50_A50_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_D50_A50 <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_D50_A50 <- start_D50_A50 %>%
  filter_all(any_vars(str_detect(., pattern = "D50 Att")))

# filter match status #
start_D50_A50 <- start_D50_A50 %>%
  filter_all(any_vars(str_detect(., pattern = "Losing")))

# filter end location #
start_D50_A50 <- start_D50_A50 %>%
  filter_all(any_vars(str_detect(., pattern = "A50")))

# create data frame #
start_D50_A50 <- as.data.frame(nrow(start_D50_A50))

return(start_D50_A50)
}

start_D50_A50_list <- list()
for (i in seq_along(game_list)){
  start_D50_A50_list[[i]] <- start_D50_A50_f(game_list[[i]])
}
Count possessions with start location in D50 and end location A25 when losing
# D50 A25 #
start_D50_A25_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_D50_A25 <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_D50_A25 <- start_D50_A25 %>%
  filter_all(any_vars(str_detect(., pattern = "D50 Att")))

# filter match status #
start_D50_A25 <- start_D50_A25 %>%
  filter_all(any_vars(str_detect(., pattern = "Losing")))

# filter end location #
start_D50_A25 <- start_D50_A25 %>%
  filter_all(any_vars(str_detect(., pattern = "A25")))

# create data frame #
start_D50_A25 <- as.data.frame(nrow(start_D50_A25))

return(start_D50_A25)
}

start_D50_A25_list <- list()
for (i in seq_along(game_list)){
  start_D50_A25_list[[i]] <- start_D50_A25_f(game_list[[i]])
}
Count possessions with start location in D50 and end location AC when losing
# D50 AC #
start_D50_AC_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_D50_AC <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_D50_AC <- start_D50_AC %>%
  filter_all(any_vars(str_detect(., pattern = "D50 Att")))

# filter match status #
start_D50_AC <- start_D50_AC %>%
  filter_all(any_vars(str_detect(., pattern = "Losing")))

# filter end location #
start_D50_AC <- start_D50_AC %>%
  filter_all(any_vars(str_detect(., pattern = "AC")))

# create data frame #
start_D50_AC <- as.data.frame(nrow(start_D50_AC))

return(start_D50_AC)
}

start_D50_AC_list <- list()
for (i in seq_along(game_list)){
  start_D50_AC_list[[i]] <- start_D50_AC_f(game_list[[i]])
}
Count possessions with start location in D50 and GS when losing
# D50 GS #
start_D50_GS_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_D50_GS <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_D50_GS <- start_D50_GS %>%
  filter_all(any_vars(str_detect(., pattern = "D50 Att")))

# filter match status #
start_D50_GS <- start_D50_GS %>%
  filter_all(any_vars(str_detect(., pattern = "Losing")))

# filter GS #
start_D50_GS <- start_D50_GS %>%
  filter_all(any_vars(str_detect(., pattern = "Goal Shot")))

# create data frame #
start_D50_GS <- as.data.frame(nrow(start_D50_GS))

return(start_D50_GS)
}

start_D50_GS_list <- list()
for (i in seq_along(game_list)){
  start_D50_GS_list[[i]] <- start_D50_GS_f(game_list[[i]])
}
Join end locations into one to convert to percentages
# make one data frame out of lists #
d25 <- bind_rows(start_D50_D25_list)
d50 <- bind_rows(start_D50_D50_list)
a50 <- bind_rows(start_D50_A50_list)
a25 <- bind_rows(start_D50_A25_list)
ac <- bind_rows(start_D50_AC_list)
gs <- bind_rows(start_D50_GS_list)

# join locations into one and rename columns #
l_d50_start <- bind_cols(gs, ac, a25, a50, d50, d25) %>%
  rename("GS" = "nrow(start_D50_GS)", 
         "AC" = "nrow(start_D50_AC)", 
         "A25" = "nrow(start_D50_A25)", 
         "A50" = "nrow(start_D50_A50)", 
         "D50" = "nrow(start_D50_D50)", 
         "D25" = "nrow(start_D50_D25)")

# add total attacks #
l_d50_start <- mutate(l_d50_start, Total = (GS + AC + A25 + A50 + D50 + D25))

# convert to percentages #
l_d50_start <- mutate(l_d50_start, L_D50_GS_P = (GS/Total)*100, 
                                   L_D50_AC_P = (AC/Total)*100, 
                                   L_D50_A25_P = (A25/Total)*100, 
                                   L_D50_A50_P = (A50/Total)*100, 
                                   L_D50_D50_P = (D50/Total)*100, 
                                   L_D50_D25_P = (D25/Total)*100)

41. CALCULATE A50 END LOCATIONS WHEN LOSING

Count possessions with start location in A50 and end location D25 when losing
# A50 D25 #
start_A50_D25_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_A50_D25 <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_A50_D25 <- start_A50_D25 %>%
  filter_all(any_vars(str_detect(., pattern = "A50 Att")))

# filter match status #
start_A50_D25 <- start_A50_D25 %>%
  filter_all(any_vars(str_detect(., pattern = "Losing")))

# filter end location #
start_A50_D25 <- start_A50_D25 %>%
  filter_all(any_vars(str_detect(., pattern = "D25")))

# create data frame #
start_A50_D25 <- as.data.frame(nrow(start_A50_D25))

return(start_A50_D25)
}

start_A50_D25_list <- list()
for (i in seq_along(game_list)){
  start_A50_D25_list[[i]] <- start_A50_D25_f(game_list[[i]])
}
Count possessions with start location in A50 and end location D50 when losing
# A50 D50 #
start_A50_D50_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_A50_D50 <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_A50_D50 <- start_A50_D50 %>%
  filter_all(any_vars(str_detect(., pattern = "A50 Att")))

# filter match status #
start_A50_D50 <- start_A50_D50 %>%
  filter_all(any_vars(str_detect(., pattern = "Losing")))

# filter end location #
start_A50_D50 <- start_A50_D50 %>%
  filter_all(any_vars(str_detect(., pattern = "D50")))

# create data frame #
start_A50_D50 <- as.data.frame(nrow(start_A50_D50))

return(start_A50_D50)
}

start_A50_D50_list <- list()
for (i in seq_along(game_list)){
  start_A50_D50_list[[i]] <- start_A50_D50_f(game_list[[i]])
}
Count possessions with start location in A50 and end location A50 when losing
# A50 A50 #
start_A50_A50_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_A50_A50 <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_A50_A50 <- start_A50_A50 %>%
  filter_all(any_vars(str_detect(., pattern = "A50 Att")))

# filter match status #
start_A50_A50 <- start_A50_A50 %>%
  filter_all(any_vars(str_detect(., pattern = "Losing")))

# filter end location #
start_A50_A50 <- start_A50_A50 %>%
  filter_all(any_vars(str_detect(., pattern = "A50")))

# create data frame #
start_A50_A50 <- as.data.frame(nrow(start_A50_A50))

return(start_A50_A50)
}

start_A50_A50_list <- list()
for (i in seq_along(game_list)){
  start_A50_A50_list[[i]] <- start_A50_A50_f(game_list[[i]])
}
Count possessions with start location in A50 and end location A25 when losing
# A50 A25 #
start_A50_A25_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_A50_A25 <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_A50_A25 <- start_A50_A25 %>%
  filter_all(any_vars(str_detect(., pattern = "A50 Att")))

# filter match status #
start_A50_A25 <- start_A50_A25 %>%
  filter_all(any_vars(str_detect(., pattern = "Losing")))

# filter end location #
start_A50_A25 <- start_A50_A25 %>%
  filter_all(any_vars(str_detect(., pattern = "A25")))

# create data frame #
start_A50_A25 <- as.data.frame(nrow(start_A50_A25))

return(start_A50_A25)
}

start_A50_A25_list <- list()
for (i in seq_along(game_list)){
  start_A50_A25_list[[i]] <- start_A50_A25_f(game_list[[i]])
}
Count possessions with start location in A50 and end location AC when losing
# A50 AC #
start_A50_AC_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_A50_AC <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_A50_AC <- start_A50_AC %>%
  filter_all(any_vars(str_detect(., pattern = "A50 Att")))

# filter match status #
start_A50_AC <- start_A50_AC %>%
  filter_all(any_vars(str_detect(., pattern = "Losing")))

# filter end location #
start_A50_AC <- start_A50_AC %>%
  filter_all(any_vars(str_detect(., pattern = "AC")))

# create data frame #
start_A50_AC <- as.data.frame(nrow(start_A50_AC))

return(start_A50_AC)
}

start_A50_AC_list <- list()
for (i in seq_along(game_list)){
  start_A50_AC_list[[i]] <- start_A50_AC_f(game_list[[i]])
}
Count possessions with start location in A50 and GS when losing
# A50 GS #
start_A50_GS_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_A50_GS <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_A50_GS <- start_A50_GS %>%
  filter_all(any_vars(str_detect(., pattern = "A50 Att")))

# filter match status #
start_A50_GS <- start_A50_GS %>%
  filter_all(any_vars(str_detect(., pattern = "Losing")))

# filter GS #
start_A50_GS <- start_A50_GS %>%
  filter_all(any_vars(str_detect(., pattern = "Goal Shot")))

# create data frame #
start_A50_GS <- as.data.frame(nrow(start_A50_GS))

return(start_A50_GS)
}

start_A50_GS_list <- list()
for (i in seq_along(game_list)){
  start_A50_GS_list[[i]] <- start_A50_GS_f(game_list[[i]])
}
Join end locations into one to convert to percentages
# make one data frame out of lists #
d25 <- bind_rows(start_A50_D25_list)
d50 <- bind_rows(start_A50_D50_list)
a50 <- bind_rows(start_A50_A50_list)
a25 <- bind_rows(start_A50_A25_list)
ac <- bind_rows(start_A50_AC_list)
gs <- bind_rows(start_A50_GS_list)

# join locations into one and rename columns #
l_a50_start <- bind_cols(gs, ac, a25, a50, d50, d25) %>%
  rename("GS" = "nrow(start_A50_GS)", 
         "AC" = "nrow(start_A50_AC)", 
         "A25" = "nrow(start_A50_A25)", 
         "A50" = "nrow(start_A50_A50)", 
         "D50" = "nrow(start_A50_D50)", 
         "D25" = "nrow(start_A50_D25)")

# add total attacks #
l_a50_start <- mutate(l_a50_start, Total = (GS + AC + A25 + A50 + D50 + D25))

# convert to percentages #
l_a50_start <- mutate(l_a50_start, L_A50_GS_P = (GS/Total)*100, 
                                   L_A50_AC_P = (AC/Total)*100, 
                                   L_A50_A25_P = (A25/Total)*100, 
                                   L_A50_A50_P = (A50/Total)*100, 
                                   L_A50_D50_P = (D50/Total)*100, 
                                   L_A50_D25_P = (D25/Total)*100)

42. CALCULATE A25 END LOCATIONS WHEN LOSING

Count possessions with start location in A25 and end location D25 when losing
# A25 D25 #
start_A25_D25_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_A25_D25 <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_A25_D25 <- start_A25_D25 %>%
  filter_all(any_vars(str_detect(., pattern = "A25 Att")))

# filter match status #
start_A25_D25 <- start_A25_D25 %>%
  filter_all(any_vars(str_detect(., pattern = "Losing")))

# filter end location #
start_A25_D25 <- start_A25_D25 %>%
  filter_all(any_vars(str_detect(., pattern = "D25")))

# create data frame #
start_A25_D25 <- as.data.frame(nrow(start_A25_D25))

return(start_A25_D25)
}

start_A25_D25_list <- list()
for (i in seq_along(game_list)){
  start_A25_D25_list[[i]] <- start_A25_D25_f(game_list[[i]])
}
Count possessions with start location in A25 and end location D50 when losing
# A25 D50 #
start_A25_D50_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_A25_D50 <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_A25_D50 <- start_A25_D50 %>%
  filter_all(any_vars(str_detect(., pattern = "A25 Att")))

# filter match status #
start_A25_D50 <- start_A25_D50 %>%
  filter_all(any_vars(str_detect(., pattern = "Losing")))

# filter end location #
start_A25_D50 <- start_A25_D50 %>%
  filter_all(any_vars(str_detect(., pattern = "D50")))

# create data frame #
start_A25_D50 <- as.data.frame(nrow(start_A25_D50))

return(start_A25_D50)
}

start_A25_D50_list <- list()
for (i in seq_along(game_list)){
  start_A25_D50_list[[i]] <- start_A25_D50_f(game_list[[i]])
}
Count possessions with start location in A25 and end location A50 when losing
# A25 A50 #
start_A25_A50_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_A25_A50 <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_A25_A50 <- start_A25_A50 %>%
  filter_all(any_vars(str_detect(., pattern = "A25 Att")))

# filter match status #
start_A25_A50 <- start_A25_A50 %>%
  filter_all(any_vars(str_detect(., pattern = "Losing")))

# filter end location #
start_A25_A50 <- start_A25_A50 %>%
  filter_all(any_vars(str_detect(., pattern = "A50")))

# create data frame #
start_A25_A50 <- as.data.frame(nrow(start_A25_A50))

return(start_A25_A50)
}

start_A25_A50_list <- list()
for (i in seq_along(game_list)){
  start_A25_A50_list[[i]] <- start_A25_A50_f(game_list[[i]])
}
Count possessions with start location in A25 and end location A25 when losing
# A25 A25 #
start_A25_A25_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_A25_A25 <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_A25_A25 <- start_A25_A25 %>%
  filter_all(any_vars(str_detect(., pattern = "A25 Att")))

# filter match status #
start_A25_A25 <- start_A25_A25 %>%
  filter_all(any_vars(str_detect(., pattern = "Losing")))

# filter end location #
start_A25_A25 <- start_A25_A25 %>%
  filter_all(any_vars(str_detect(., pattern = "A25")))

# create data frame #
start_A25_A25 <- as.data.frame(nrow(start_A25_A25))

return(start_A25_A25)
}

start_A25_A25_list <- list()
for (i in seq_along(game_list)){
  start_A25_A25_list[[i]] <- start_A25_A25_f(game_list[[i]])
}
Count possessions with start location in A25 and end location AC when losing
# A25 AC #
start_A25_AC_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_A25_AC <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_A25_AC <- start_A25_AC %>%
  filter_all(any_vars(str_detect(., pattern = "A25 Att")))

# filter match status#
start_A25_AC <- start_A25_AC %>%
  filter_all(any_vars(str_detect(., pattern = "Losing")))

# filter end location #
start_A25_AC <- start_A25_AC %>%
  filter_all(any_vars(str_detect(., pattern = "AC")))

# create data frame #
start_A25_AC <- as.data.frame(nrow(start_A25_AC))

return(start_A25_AC)
}

start_A25_AC_list <- list()
for (i in seq_along(game_list)){
  start_A25_AC_list[[i]] <- start_A25_AC_f(game_list[[i]])
}
Count possessions with start location in A25 and GS when losing
# A25 GS #
start_A25_GS_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_A25_GS <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_A25_GS <- start_A25_GS %>%
  filter_all(any_vars(str_detect(., pattern = "A25 Att")))

# filter match status #
start_A25_GS <- start_A25_GS %>%
  filter_all(any_vars(str_detect(., pattern = "Losing")))

# filter GS #
start_A25_GS <- start_A25_GS %>%
  filter_all(any_vars(str_detect(., pattern = "Goal Shot")))

# create data frame #
start_A25_GS <- as.data.frame(nrow(start_A25_GS))

return(start_A25_GS)
}

start_A25_GS_list <- list()
for (i in seq_along(game_list)){
  start_A25_GS_list[[i]] <- start_A25_GS_f(game_list[[i]])
}
Join end locations into one to convert to percentages
# make one data frame out of lists #
d25 <- bind_rows(start_A25_D25_list)
d50 <- bind_rows(start_A25_D50_list)
a50 <- bind_rows(start_A25_A50_list)
a25 <- bind_rows(start_A25_A25_list)
ac <- bind_rows(start_A25_AC_list)
gs <- bind_rows(start_A25_GS_list)

# join locations into one and rename columns #
l_a25_start <- bind_cols(gs, ac, a25, a50, d50, d25) %>%
  rename("GS" = "nrow(start_A25_GS)", 
         "AC" = "nrow(start_A25_AC)", 
         "A25" = "nrow(start_A25_A25)", 
         "A50" = "nrow(start_A25_A50)", 
         "D50" = "nrow(start_A25_D50)", 
         "D25" = "nrow(start_A25_D25)")

# add total attacks #
l_a25_start <- mutate(l_a25_start, Total = (GS + AC + A25 + A50 + D50 + D25))

# convert to percentages #
l_a25_start <- mutate(l_a25_start, L_A25_GS_P = (GS/Total)*100, 
                                   L_A25_AC_P = (AC/Total)*100, 
                                   L_A25_A25_P = (A25/Total)*100, 
                                   L_A25_A50_P = (A50/Total)*100, 
                                   L_A25_D50_P = (D50/Total)*100, 
                                   L_A25_D25_P = (D25/Total)*100)

43. CALCULATE D25 END LOCATIONS WHEN DRAWING

Count possessions with start location in D25 and end location D25 when drawing
# D25 D25 #
start_D25_D25_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_D25_D25 <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_D25_D25 <- start_D25_D25 %>%
  filter_all(any_vars(str_detect(., pattern = "D25 Att")))

# filter match status #
start_D25_D25 <- start_D25_D25 %>%
  filter_all(any_vars(str_detect(., pattern = "Drawing")))

# filter end location #
start_D25_D25 <- start_D25_D25 %>%
  filter_all(any_vars(str_detect(., pattern = "D25")))

# create data frame #
start_D25_D25 <- as.data.frame(nrow(start_D25_D25))

return(start_D25_D25)
}

start_D25_D25_list <- list()
for (i in seq_along(game_list)){
  start_D25_D25_list[[i]] <- start_D25_D25_f(game_list[[i]])
}
Count possessions with start location in D25 and end location D50 when drawing
# D25 D50 #
start_D25_D50_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_D25_D50 <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_D25_D50 <- start_D25_D50 %>%
  filter_all(any_vars(str_detect(., pattern = "D25 Att")))

# filter match status #
start_D25_D50 <- start_D25_D50 %>%
  filter_all(any_vars(str_detect(., pattern = "Drawing")))

# filter end location #
start_D25_D50 <- start_D25_D50 %>%
  filter_all(any_vars(str_detect(., pattern = "D50")))

# create data frame #
start_D25_D50 <- as.data.frame(nrow(start_D25_D50))

return(start_D25_D50)
}

start_D25_D50_list <- list()
for (i in seq_along(game_list)){
  start_D25_D50_list[[i]] <- start_D25_D50_f(game_list[[i]])
}
Count possessions with start location in D25 and end location A50 when drawing
# D25 A50 #
start_D25_A50_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_D25_A50 <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_D25_A50 <- start_D25_A50 %>%
  filter_all(any_vars(str_detect(., pattern = "D25 Att")))

# filter match status #
start_D25_A50 <- start_D25_A50 %>%
  filter_all(any_vars(str_detect(., pattern = "Drawing")))

# filter end location #
start_D25_A50 <- start_D25_A50 %>%
  filter_all(any_vars(str_detect(., pattern = "A50")))

# create data frame #
start_D25_A50 <- as.data.frame(nrow(start_D25_A50))

return(start_D25_A50)
}

start_D25_A50_list <- list()
for (i in seq_along(game_list)){
  start_D25_A50_list[[i]] <- start_D25_A50_f(game_list[[i]])
}
Count possessions with start location in D25 and end location A25 when drawing
# D25 A25 #
start_D25_A25_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_D25_A25 <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_D25_A25 <- start_D25_A25 %>%
  filter_all(any_vars(str_detect(., pattern = "D25 Att")))

# filter match status #
start_D25_A25 <- start_D25_A25 %>%
  filter_all(any_vars(str_detect(., pattern = "Drawing")))

# filter end location #
start_D25_A25 <- start_D25_A25 %>%
  filter_all(any_vars(str_detect(., pattern = "A25")))

# create data frame #
start_D25_A25 <- as.data.frame(nrow(start_D25_A25))

return(start_D25_A25)
}

start_D25_A25_list <- list()
for (i in seq_along(game_list)){
  start_D25_A25_list[[i]] <- start_D25_A25_f(game_list[[i]])
}
Count possessions with start location in D25 and end location AC when drawing
# D25 AC #
start_D25_AC_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_D25_AC <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_D25_AC <- start_D25_AC %>%
  filter_all(any_vars(str_detect(., pattern = "D25 Att")))

# filter match status #
start_D25_AC <- start_D25_AC %>%
  filter_all(any_vars(str_detect(., pattern = "Drawing")))

# filter end location #
start_D25_AC <- start_D25_AC %>%
  filter_all(any_vars(str_detect(., pattern = "AC")))

# create data frame #
start_D25_AC <- as.data.frame(nrow(start_D25_AC))

return(start_D25_AC)
}

start_D25_AC_list <- list()
for (i in seq_along(game_list)){
  start_D25_AC_list[[i]] <- start_D25_AC_f(game_list[[i]])
}
Count possessions with start location in D25 and GS when drawing
# D25 GS #
start_D25_GS_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_D25_GS <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_D25_GS <- start_D25_GS %>%
  filter_all(any_vars(str_detect(., pattern = "D25 Att")))

# filter match status #
start_D25_GS <- start_D25_GS %>%
  filter_all(any_vars(str_detect(., pattern = "Drawing")))

# filter GS #
start_D25_GS <- start_D25_GS %>%
  filter_all(any_vars(str_detect(., pattern = "Goal Shot")))

# create data frame #
start_D25_GS <- as.data.frame(nrow(start_D25_GS))

return(start_D25_GS)
}

start_D25_GS_list <- list()
for (i in seq_along(game_list)){
  start_D25_GS_list[[i]] <- start_D25_GS_f(game_list[[i]])
}
Join end locations into one to convert to percentages
# make one data frame out of lists #
d25 <- bind_rows(start_D25_D25_list)
d50 <- bind_rows(start_D25_D50_list)
a50 <- bind_rows(start_D25_A50_list)
a25 <- bind_rows(start_D25_A25_list)
ac <- bind_rows(start_D25_AC_list)
gs <- bind_rows(start_D25_GS_list)

# join locations into one and rename columns #
d_d25_start <- bind_cols(gs, ac, a25, a50, d50, d25) %>%
  rename("GS" = "nrow(start_D25_GS)", 
         "AC" = "nrow(start_D25_AC)", 
         "A25" = "nrow(start_D25_A25)", 
         "A50" = "nrow(start_D25_A50)", 
         "D50" = "nrow(start_D25_D50)", 
         "D25" = "nrow(start_D25_D25)")

# add total attacks #
d_d25_start <- mutate(d_d25_start, Total = (GS + AC + A25 + A50 + D50 + D25))

# convert to percentages #
d_d25_start <- mutate(d_d25_start, D_D25_GS_P = (GS/Total)*100, 
                                   D_D25_AC_P = (AC/Total)*100, 
                                   D_D25_A25_P = (A25/Total)*100,
                                   D_D25_A50_P = (A50/Total)*100, 
                                   D_D25_D50_P = (D50/Total)*100, 
                                   D_D25_D25_P = (D25/Total)*100)

44. CALCULATE D50 END LOCATIONS WHEN DRAWING

Count possessions with start location in D50 and end location D25 when drawing
# D50 D25 #
start_D50_D25_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_D50_D25 <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_D50_D25 <- start_D50_D25 %>%
  filter_all(any_vars(str_detect(., pattern = "D50 Att")))

# filter match status #
start_D50_D25 <- start_D50_D25 %>%
  filter_all(any_vars(str_detect(., pattern = "Drawing")))

# filter end location #
start_D50_D25 <- start_D50_D25 %>%
  filter_all(any_vars(str_detect(., pattern = "D25")))

# create data frame #
start_D50_D25 <- as.data.frame(nrow(start_D50_D25))

return(start_D50_D25)
}

start_D50_D25_list <- list()
for (i in seq_along(game_list)){
  start_D50_D25_list[[i]] <- start_D50_D25_f(game_list[[i]])
}
Count possessions with start location in D50 and end location D50 when drawing
# D50 D50 #
start_D50_D50_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_D50_D50 <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_D50_D50 <- start_D50_D50 %>%
  filter_all(any_vars(str_detect(., pattern = "D50 Att")))

# filter match status #
start_D50_D50 <- start_D50_D50 %>%
  filter_all(any_vars(str_detect(., pattern = "Drawing")))

# filter end location #
start_D50_D50 <- start_D50_D50 %>%
  filter_all(any_vars(str_detect(., pattern = "D50")))

# create data frame #
start_D50_D50 <- as.data.frame(nrow(start_D50_D50))

return(start_D50_D50)
}

start_D50_D50_list <- list()
for (i in seq_along(game_list)){
  start_D50_D50_list[[i]] <- start_D50_D50_f(game_list[[i]])
}
Count possessions with start location in D50 and end location A50 when drawing
# D50 A50 #
start_D50_A50_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_D50_A50 <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_D50_A50 <- start_D50_A50 %>%
  filter_all(any_vars(str_detect(., pattern = "D50 Att")))

# filter match status #
start_D50_A50 <- start_D50_A50 %>%
  filter_all(any_vars(str_detect(., pattern = "Drawing")))

# filter end location #
start_D50_A50 <- start_D50_A50 %>%
  filter_all(any_vars(str_detect(., pattern = "A50")))

# create data frame #
start_D50_A50 <- as.data.frame(nrow(start_D50_A50))

return(start_D50_A50)
}

start_D50_A50_list <- list()
for (i in seq_along(game_list)){
  start_D50_A50_list[[i]] <- start_D50_A50_f(game_list[[i]])
}
Count possessions with start location in D50 and end location A25 when drawing
# D50 A25 #
start_D50_A25_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_D50_A25 <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_D50_A25 <- start_D50_A25 %>%
  filter_all(any_vars(str_detect(., pattern = "D50 Att")))

# filter match status #
start_D50_A25 <- start_D50_A25 %>%
  filter_all(any_vars(str_detect(., pattern = "Drawing")))

# filter end location #
start_D50_A25 <- start_D50_A25 %>%
  filter_all(any_vars(str_detect(., pattern = "A25")))

# create data frame #
start_D50_A25 <- as.data.frame(nrow(start_D50_A25))

return(start_D50_A25)
}

start_D50_A25_list <- list()
for (i in seq_along(game_list)){
  start_D50_A25_list[[i]] <- start_D50_A25_f(game_list[[i]])
}
Count possessions with start location in D50 and end location AC when drawing
# D50 AC #
start_D50_AC_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_D50_AC <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_D50_AC <- start_D50_AC %>%
  filter_all(any_vars(str_detect(., pattern = "D50 Att")))

# filter match status #
start_D50_AC <- start_D50_AC %>%
  filter_all(any_vars(str_detect(., pattern = "Drawing")))

# filter end location #
start_D50_AC <- start_D50_AC %>%
  filter_all(any_vars(str_detect(., pattern = "AC")))

# create data frame #
start_D50_AC <- as.data.frame(nrow(start_D50_AC))

return(start_D50_AC)
}

start_D50_AC_list <- list()
for (i in seq_along(game_list)){
  start_D50_AC_list[[i]] <- start_D50_AC_f(game_list[[i]])
}
Count possessions with start location in D50 and GS when drawing
# D50 GS #
start_D50_GS_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_D50_GS <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_D50_GS <- start_D50_GS %>%
  filter_all(any_vars(str_detect(., pattern = "D50 Att")))

# filter match status #
start_D50_GS <- start_D50_GS %>%
  filter_all(any_vars(str_detect(., pattern = "Drawing")))

# filter GS #
start_D50_GS <- start_D50_GS %>%
  filter_all(any_vars(str_detect(., pattern = "Goal Shot")))

# create data frame #
start_D50_GS <- as.data.frame(nrow(start_D50_GS))

return(start_D50_GS)
}

start_D50_GS_list <- list()
for (i in seq_along(game_list)){
  start_D50_GS_list[[i]] <- start_D50_GS_f(game_list[[i]])
}
Join end locations into one to convert to percentages
# make one data frame out of lists #
d25 <- bind_rows(start_D50_D25_list)
d50 <- bind_rows(start_D50_D50_list)
a50 <- bind_rows(start_D50_A50_list)
a25 <- bind_rows(start_D50_A25_list)
ac <- bind_rows(start_D50_AC_list)
gs <- bind_rows(start_D50_GS_list)

# join locations into one and rename columns #
d_d50_start <- bind_cols(gs, ac, a25, a50, d50, d25) %>%
  rename("GS" = "nrow(start_D50_GS)", 
         "AC" = "nrow(start_D50_AC)", 
         "A25" = "nrow(start_D50_A25)", 
         "A50" = "nrow(start_D50_A50)", 
         "D50" = "nrow(start_D50_D50)", 
         "D25" = "nrow(start_D50_D25)")

# add total attacks #
d_d50_start <- mutate(d_d50_start, Total = (GS + AC + A25 + A50 + D50 + D25))

# convert to percentages #
d_d50_start <- mutate(d_d50_start, D_D50_GS_P = (GS/Total)*100, 
                                   D_D50_AC_P = (AC/Total)*100, 
                                   D_D50_A25_P = (A25/Total)*100, 
                                   D_D50_A50_P = (A50/Total)*100, 
                                   D_D50_D50_P = (D50/Total)*100, 
                                   D_D50_D25_P = (D25/Total)*100)

45. CALCULATE A50 END LOCATIONS WHEN DRAWING

Count possessions with start location in A50 and end location D25 when drawing
# A50 D25 #
start_A50_D25_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_A50_D25 <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_A50_D25 <- start_A50_D25 %>%
  filter_all(any_vars(str_detect(., pattern = "A50 Att")))

# filter match status #
start_A50_D25 <- start_A50_D25 %>%
  filter_all(any_vars(str_detect(., pattern = "Drawing")))

# filter end location #
start_A50_D25 <- start_A50_D25 %>%
  filter_all(any_vars(str_detect(., pattern = "D25")))

# create data frame #
start_A50_D25 <- as.data.frame(nrow(start_A50_D25))

return(start_A50_D25)
}

start_A50_D25_list <- list()
for (i in seq_along(game_list)){
  start_A50_D25_list[[i]] <- start_A50_D25_f(game_list[[i]])
}
Count possessions with start location in A50 and end location D25 when drawing
# A50 D50 #
start_A50_D50_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_A50_D50 <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_A50_D50 <- start_A50_D50 %>%
  filter_all(any_vars(str_detect(., pattern = "A50 Att")))

# filter match status #
start_A50_D50 <- start_A50_D50 %>%
  filter_all(any_vars(str_detect(., pattern = "Drawing")))

# filter end location #
start_A50_D50 <- start_A50_D50 %>%
  filter_all(any_vars(str_detect(., pattern = "D50")))

# create data frame #
start_A50_D50 <- as.data.frame(nrow(start_A50_D50))

return(start_A50_D50)
}

start_A50_D50_list <- list()
for (i in seq_along(game_list)){
  start_A50_D50_list[[i]] <- start_A50_D50_f(game_list[[i]])
}
Count possessions with start location in A50 and end location A50 when drawing
# A50 A50 #
start_A50_A50_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_A50_A50 <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_A50_A50 <- start_A50_A50 %>%
  filter_all(any_vars(str_detect(., pattern = "A50 Att")))

# filter match status #
start_A50_A50 <- start_A50_A50 %>%
  filter_all(any_vars(str_detect(., pattern = "Drawing")))

# filter end location #
start_A50_A50 <- start_A50_A50 %>%
  filter_all(any_vars(str_detect(., pattern = "A50")))

# create data frame #
start_A50_A50 <- as.data.frame(nrow(start_A50_A50))

return(start_A50_A50)
}

start_A50_A50_list <- list()
for (i in seq_along(game_list)){
  start_A50_A50_list[[i]] <- start_A50_A50_f(game_list[[i]])
}
Count possessions with start location in A50 and end location A25 when drawing
# A50 A25 #
start_A50_A25_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_A50_A25 <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_A50_A25 <- start_A50_A25 %>%
  filter_all(any_vars(str_detect(., pattern = "A50 Att")))

# filter match status #
start_A50_A25 <- start_A50_A25 %>%
  filter_all(any_vars(str_detect(., pattern = "Drawing")))

# filter end location #
start_A50_A25 <- start_A50_A25 %>%
  filter_all(any_vars(str_detect(., pattern = "A25")))

# create data frame #
start_A50_A25 <- as.data.frame(nrow(start_A50_A25))

return(start_A50_A25)
}

start_A50_A25_list <- list()
for (i in seq_along(game_list)){
  start_A50_A25_list[[i]] <- start_A50_A25_f(game_list[[i]])
}
Count possessions with start location in A50 and end location AC when drawing
# A50 AC #
start_A50_AC_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_A50_AC <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_A50_AC <- start_A50_AC %>%
  filter_all(any_vars(str_detect(., pattern = "A50 Att")))

# filter match status #
start_A50_AC <- start_A50_AC %>%
  filter_all(any_vars(str_detect(., pattern = "Drawing")))

# filter end location #
start_A50_AC <- start_A50_AC %>%
  filter_all(any_vars(str_detect(., pattern = "AC")))

# create data frame #
start_A50_AC <- as.data.frame(nrow(start_A50_AC))

return(start_A50_AC)
}

start_A50_AC_list <- list()
for (i in seq_along(game_list)){
  start_A50_AC_list[[i]] <- start_A50_AC_f(game_list[[i]])
}
Count possessions with start location in A50 and GS when drawing
# A50 GS #
start_A50_GS_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_A50_GS <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_A50_GS <- start_A50_GS %>%
  filter_all(any_vars(str_detect(., pattern = "A50 Att")))

# filter match status #
start_A50_GS <- start_A50_GS %>%
  filter_all(any_vars(str_detect(., pattern = "Drawing")))

# filter GS #
start_A50_GS <- start_A50_GS %>%
  filter_all(any_vars(str_detect(., pattern = "Goal Shot")))

# create data frame #
start_A50_GS <- as.data.frame(nrow(start_A50_GS))
 
return(start_A50_GS)
}

start_A50_GS_list <- list()
for (i in seq_along(game_list)){
  start_A50_GS_list[[i]] <- start_A50_GS_f(game_list[[i]])
}
Join end locations into one to convert to percentages
# make one data frame out of lists #
d25 <- bind_rows(start_A50_D25_list)
d50 <- bind_rows(start_A50_D50_list)
a50 <- bind_rows(start_A50_A50_list)
a25 <- bind_rows(start_A50_A25_list)
ac <- bind_rows(start_A50_AC_list)
gs <- bind_rows(start_A50_GS_list)

# join locations into one and rename columns #
d_a50_start <- bind_cols(gs, ac, a25, a50, d50, d25) %>%
  rename("GS" = "nrow(start_A50_GS)", 
         "AC" = "nrow(start_A50_AC)", 
         "A25" = "nrow(start_A50_A25)", 
         "A50" = "nrow(start_A50_A50)", 
         "D50" = "nrow(start_A50_D50)", 
         "D25" = "nrow(start_A50_D25)")

# add total attacks #
d_a50_start <- mutate(d_a50_start, Total = (GS + AC + A25 + A50 + D50 + D25))

# convert to percentages #
d_a50_start <- mutate(d_a50_start, D_A50_GS_P = (GS/Total)*100, 
                                   D_A50_AC_P = (AC/Total)*100, 
                                   D_A50_A25_P = (A25/Total)*100, 
                                   D_A50_A50_P = (A50/Total)*100, 
                                   D_A50_D50_P = (D50/Total)*100, 
                                   D_A50_D25_P = (D25/Total)*100)

46. CALCULATE A25 END LOCATIONS WHEN DRAWING

Count possessions with start location in A25 and end location D25 when drawing
# A25 D25 #
start_A25_D25_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_A25_D25 <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_A25_D25 <- start_A25_D25 %>%
  filter_all(any_vars(str_detect(., pattern = "A25 Att")))

# filter match status #
start_A25_D25 <- start_A25_D25 %>%
  filter_all(any_vars(str_detect(., pattern = "Drawing")))

# filter end location #
start_A25_D25 <- start_A25_D25 %>%
  filter_all(any_vars(str_detect(., pattern = "D25")))

# create data frame #
start_A25_D25 <- as.data.frame(nrow(start_A25_D25))

return(start_A25_D25)
}

start_A25_D25_list <- list()
for (i in seq_along(game_list)){
  start_A25_D25_list[[i]] <- start_A25_D25_f(game_list[[i]])
}
Count possessions with start location in A25 and end location D50 when drawing
# A25 D50 #
start_A25_D50_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_A25_D50 <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_A25_D50 <- start_A25_D50 %>%
  filter_all(any_vars(str_detect(., pattern = "A25 Att")))

# filter match status #
start_A25_D50 <- start_A25_D50 %>%
  filter_all(any_vars(str_detect(., pattern = "Drawing")))

# filter end location #
start_A25_D50 <- start_A25_D50 %>%
  filter_all(any_vars(str_detect(., pattern = "D50")))

# create data frame #
start_A25_D50 <- as.data.frame(nrow(start_A25_D50))

return(start_A25_D50)
}

start_A25_D50_list <- list()
for (i in seq_along(game_list)){
  start_A25_D50_list[[i]] <- start_A25_D50_f(game_list[[i]])
}
Count possessions with start location in A25 and end location A50 when drawing
# A25 A50 #
start_A25_A50_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_A25_A50 <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_A25_A50 <- start_A25_A50 %>%
  filter_all(any_vars(str_detect(., pattern = "A25 Att")))

# filter match status #
start_A25_A50 <- start_A25_A50 %>%
  filter_all(any_vars(str_detect(., pattern = "Drawing")))

# filter end location #
start_A25_A50 <- start_A25_A50 %>%
  filter_all(any_vars(str_detect(., pattern = "A50")))

# create data frame #
start_A25_A50 <- as.data.frame(nrow(start_A25_A50))

return(start_A25_A50)
}

start_A25_A50_list <- list()
for (i in seq_along(game_list)){
  start_A25_A50_list[[i]] <- start_A25_A50_f(game_list[[i]])
}
Count possessions with start location in A25 and end location A25 when drawing
# A25 A25 #
start_A25_A25_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_A25_A25 <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_A25_A25 <- start_A25_A25 %>%
  filter_all(any_vars(str_detect(., pattern = "A25 Att")))

# filter match status #
start_A25_A25 <- start_A25_A25 %>%
  filter_all(any_vars(str_detect(., pattern = "Drawing")))

# filter end location #
start_A25_A25 <- start_A25_A25 %>%
  filter_all(any_vars(str_detect(., pattern = "A25")))

# create data frame #
start_A25_A25 <- as.data.frame(nrow(start_A25_A25))

return(start_A25_A25)
}

start_A25_A25_list <- list()
for (i in seq_along(game_list)){
  start_A25_A25_list[[i]] <- start_A25_A25_f(game_list[[i]])
}
Count possessions with start location in A25 and end location AC when drawing
# A25 AC #
start_A25_AC_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_A25_AC <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_A25_AC <- start_A25_AC %>%
  filter_all(any_vars(str_detect(., pattern = "A25 Att")))

# filter match status #
start_A25_AC <- start_A25_AC %>%
  filter_all(any_vars(str_detect(., pattern = "Drawing")))

# filter end location #
start_A25_AC <- start_A25_AC %>%
  filter_all(any_vars(str_detect(., pattern = "AC")))

# create data frame #
start_A25_AC <- as.data.frame(nrow(start_A25_AC))

return(start_A25_AC)
}

start_A25_AC_list <- list()
for (i in seq_along(game_list)){
  start_A25_AC_list[[i]] <- start_A25_AC_f(game_list[[i]])
}
Count possessions with start location in A25 and GS when drawing
# A25 GS #
start_A25_GS_f <- function(x){
# extract from all data (Game) rows referring to Attack and save as separate data frame #
start_A25_GS <- filter(x, A == "Attack" & is.na(B))
  
# filter start location #
start_A25_GS <- start_A25_GS %>%
  filter_all(any_vars(str_detect(., pattern = "A25 Att")))

# filter match status #
start_A25_GS <- start_A25_GS %>%
  filter_all(any_vars(str_detect(., pattern = "Drawing")))

# filter GS #
start_A25_GS <- start_A25_GS %>%
  filter_all(any_vars(str_detect(., pattern = "Goal Shot")))

# create data frame #
start_A25_GS <- as.data.frame(nrow(start_A25_GS))

return(start_A25_GS)
}

start_A25_GS_list <- list()
for (i in seq_along(game_list)){
  start_A25_GS_list[[i]] <- start_A25_GS_f(game_list[[i]])
}
Join end locations into one to convert to percentages
# make one data frame out of lists #
d25 <- bind_rows(start_A25_D25_list)
d50 <- bind_rows(start_A25_D50_list)
a50 <- bind_rows(start_A25_A50_list)
a25 <- bind_rows(start_A25_A25_list)
ac <- bind_rows(start_A25_AC_list)
gs <- bind_rows(start_A25_GS_list)

# join locations into one and rename columns #
d_a25_start <- bind_cols(gs, ac, a25, a50, d50, d25) %>%
  rename("GS" = "nrow(start_A25_GS)", 
         "AC" = "nrow(start_A25_AC)", 
         "A25" = "nrow(start_A25_A25)", 
         "A50" = "nrow(start_A25_A50)", 
         "D50" = "nrow(start_A25_D50)", 
         "D25" = "nrow(start_A25_D25)")

# add total attacks #
d_a25_start <- mutate(d_a25_start, Total = (GS + AC + A25 + A50 + D50 + D25))

# convert to percentages #
d_a25_start <- mutate(d_a25_start, D_A25_GS_P = (GS/Total)*100, 
                                   D_A25_AC_P = (AC/Total)*100, 
                                   D_A25_A25_P = (A25/Total)*100, 
                                   D_A25_A50_P = (A50/Total)*100, 
                                   D_A25_D50_P = (D50/Total)*100, 
                                   D_A25_D25_P = (D25/Total)*100)

47. EXPORT DATA

Calculate the league average for all teams end locations per start location per match status and tidy table for presentation
# select columns for analysis #
win_a25 <- select(w_a25_start, Team, W_A25_GS_P, W_A25_AC_P, W_A25_A25_P, W_A25_A50_P, W_A25_D50_P, W_A25_D25_P)

win_a50 <- select(w_a50_start, W_A50_GS_P, W_A50_AC_P, W_A50_A25_P, W_A50_A50_P, W_A50_D50_P, W_A50_D25_P)

win_d50 <- select(w_d50_start,  W_D50_GS_P, W_D50_AC_P, W_D50_A25_P, W_D50_A50_P, W_D50_D50_P, W_D50_D25_P)

win_d25 <- select(w_d25_start, W_D25_GS_P, W_D25_AC_P, W_D25_A25_P, W_D25_A50_P, W_D25_D50_P, W_D25_D25_P)

lose_a25 <- select(l_a25_start, L_A25_GS_P, L_A25_AC_P, L_A25_A25_P, L_A25_A50_P, L_A25_D50_P, L_A25_D25_P)

lose_a50 <- select(l_a50_start, L_A50_GS_P, L_A50_AC_P, L_A50_A25_P, L_A50_A50_P, L_A50_D50_P, L_A50_D25_P)

lose_d50 <- select(l_d50_start,  L_D50_GS_P, L_D50_AC_P, L_D50_A25_P, L_D50_A50_P, L_D50_D50_P, L_D50_D25_P)

lose_d25 <- select(l_d25_start, L_D25_GS_P, L_D25_AC_P, L_D25_A25_P, L_D25_A50_P, L_D25_D50_P, L_D25_D25_P)

draw_a25 <- select(d_a25_start,  D_A25_GS_P, D_A25_AC_P, D_A25_A25_P, D_A25_A50_P, D_A25_D50_P, D_A25_D25_P)

draw_a50 <- select(d_a50_start, D_A50_GS_P, D_A50_AC_P, D_A50_A25_P, D_A50_A50_P, D_A50_D50_P, D_A50_D25_P)

draw_d50 <- select(d_d50_start,  D_D50_GS_P, D_D50_AC_P, D_D50_A25_P, D_D50_A50_P, D_D50_D50_P, D_D50_D25_P)

draw_d25 <- select(d_d25_start, D_D25_GS_P, D_D25_AC_P, D_D25_A25_P, D_D25_A50_P, D_D25_D50_P, D_D25_D25_P)

# join match status locations into one #
end_long <- bind_cols(win_a25, win_a50, win_d50, win_d25, lose_a25, lose_a50, lose_d50, lose_d25, draw_a25, draw_a50, draw_d50, draw_d25)

# calculate average per variable #
end_avg <- data.frame(Percent = colMeans(end_long[, c(-1)]))

# add variable names #
end_avg <- rownames_to_column(end_avg, var = "End")
end_avg <- separate(end_avg, End, c("Match.Status", "Start", "End"))

# save data frame #
write_xlsx(end_avg, "Data/Processed Data//End_Avg_Women.xlsx")

end_avg2 <- head(end_avg)
knitr::kable(end_avg2, caption = "League End Location Averages")
League End Location Averages
Match.Status Start End Percent
W A25 GS 10.4887163
W A25 AC 28.0556021
W A25 A25 58.4230357
W A25 A50 2.3646331
W A25 D50 0.6680129
W A25 D25 0.0000000
Calculate team averages for all end locations per start location per match status and tidy table for presentation
# transform from wide to long #
end_long <- end_long %>%
  pivot_longer(cols = W_A25_GS_P:D_D25_D25_P,
               names_to = "End",
               values_to = "Percent")

# separate descriptive column #
end_long <- separate(end_long, End, c("Match.Status", "Start", "End"))

# save data frame #
write_xlsx(end_long, "Data/Processed Data//End_Women.xlsx")

end_long2 <- head(end_long)
knitr::kable(end_long2, caption = "Team End Location Averages")
Team End Location Averages
Team Match.Status Start End Percent
Argentina W A25 GS 13.559322
Argentina W A25 AC 27.118644
Argentina W A25 A25 55.932203
Argentina W A25 A50 1.694915
Argentina W A25 D50 1.694915
Argentina W A25 D25 0.000000