TPD-InternationalFootballRe.../Server/SummaryStatistics.R

98 lines
2.6 KiB
R
Raw Normal View History

2019-05-19 19:05:06 +02:00
#
# 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
)
2019-06-14 23:50:49 +02:00
return(goals_lost)
2019-05-19 19:05:06 +02:00
}
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)
2019-06-14 23:50:49 +02:00
}
# Summary
goalsOverallSummary <- function(matches, team) {
scored <- goalsScored(matches, team)
scoredAvg <- avgGoalsScored(matches, team)
lost <- goalsLost(matches, team)
lostAvg <- avgGoalsLost(matches, team)
goalsOverall <- data.frame(
"Home" = c(scored$home_goals, lost$home_goals, scoredAvg$home_goals_avg, lostAvg$home_goals_avg),
"Away" = c(scored$away_goals, lost$away_goals, scoredAvg$away_goals_avg, lostAvg$away_goals_avg),
"Overall" = c(scored$overall, lost$overall, scoredAvg$overall_avg, lostAvg$overall_avg),
stringsAsFactors = FALSE
)
row.names(goalsOverall) <- c("Scored","Lost", "Scored Avg", "Lost Avg")
return(goalsOverall)
2019-05-19 19:05:06 +02:00
}