# # Summary statistics # # Sum goalsScored <- function(matches, team) { home_goals <- matches %>% filter(home_team == team) %>% select(home_score) %>% sum() away_goals <- matches %>% filter(away_team == team) %>% select(away_score) %>% sum() goals_scored <- tribble( ~home_goals, ~away_goals, ~overall, home_goals, away_goals, home_goals + away_goals ) return(goals_scored) } goalsLost <- function(matches, team) { home_goals <- matches %>% filter(home_team != team) %>% select(home_score) %>% sum() away_goals <- matches %>% filter(away_team != team) %>% select(away_score) %>% sum() goals_lost <- tribble( ~home_goals, ~away_goals, ~overall, home_goals, away_goals, home_goals + away_goals ) return(goals_scored) } matchesNumber <- function(matches, team) { home_matches <- matches %>% filter(home_team == team) %>% nrow() away_matches <- matches %>% filter(away_team == team) %>% nrow() matches_number <- tribble( ~home_matches, ~away_matches, ~overall, home_matches, away_matches, home_matches + away_matches ) return(matches_number) } # Mean avgGoalsScored <- function(matches, team) { goals_scored <- goalsScored(matches, team) matches_number <- matchesNumber(matches, team) avg_goals_scored <- tribble( ~home_goals_avg, ~away_goals_avg, ~overall_avg, goals_scored$home_goals / matches_number$home_matches, goals_scored$away_goals / matches_number$away_matches, goals_scored$overall / matches_number$overall ) return(avg_goals_scored) } avgGoalsLost <- function(matches, team) { goals_lost <- goalsLost(matches, team) matches_number <- matchesNumber(matches, team) avg_goals_lost <- tribble( ~home_goals_avg, ~away_goals_avg, ~overall_avg, goals_lost$home_goals / matches_number$home_matches, goals_lost$away_goals / matches_number$away_matches, goals_lost$overall / matches_number$overall ) return(avg_goals_lost) }