rm(list = ls())
library(tidyverse)
Directory = "D:/Dropbox/Dropbox/Wits 2021/FINE4020/R Group Project/EPL/"
Season_1011 <- read_csv(paste0(Directory, "season-1011_csv.csv"), col_types = "cDcciiciicciiiiiiiiiiii")
# Season_1011 <- read_csv(paste0(Directory, "season-1011_csv.csv"))
RefStats1011 <- Season_1011[,c(2,11,16,17,20,21,22,23)]
RefStats1011[,3:8] <- sapply(RefStats1011[,3:8], as.numeric)
Referees1011 <- unique(Season_1011$Referee)
# Referees1011 <- Season_1011 %>% 
#   count(Referee) %>% 
#   select(1)
Marriner1011 <- RefStats1011[(which(RefStats1011$Referee==Referees1011[1,])),]
karabo_method = function(name, df = RefStats1011){
  df = df %>% 
    filter(Referee == name)
  Fouls <- sum(df[,3:4])
  Yellows <- sum(df[,5:6])
  Reds <- sum(df[,7:8])
  YellowRatio <- Yellows/Fouls
  RedRatio <- Reds/Fouls
  Games <- length(df[,1])
  Ref <- df[2,2]
  RefTable <- c(Ref, Games, Fouls, Yellows, Reds, YellowRatio, RedRatio)
  return(RefTable)
}
Marriner1011Summary <- karabo_method("A Marriner")
Ref_Summary <- map_dfr(Referees1011, karabo_method)
Ref_Summary <- RefStats1011 %>% 
  group_by(Referee) %>% 
  summarise(Fouls = sum(AF, HF),
            Yellows = sum(HY, AY),
            Reds = sum(HR, AR),
            Games = n()) %>% 
  mutate(YellowRatio = Yellows/Fouls,
         RedRatio = Reds/Fouls)