R/get_latest_standings.R

Defines functions get_latest_standings

Documented in get_latest_standings

#' @title Getting the Latest Standings of the Competition
#'
#' @description This function calculates the latest standings in my PremPredict competition.
#' @param data_input A data frame that is created by the get_player_data function.
#' @param ... Extra parameters.
#' @keywords import
#' @export
#' @examples
#' \dontrun{
#' get_latest_standings(data_input = data_from_get_player_data)
#' }

get_latest_standings <- function(data_input, ...){

  clubOrder <- get_latest_EPL_table(...)

  clubsABC <- sort(clubOrder$Team)

  clubStandings <- match(data_input$Club, clubOrder$Team, 0)
  topClub <- match(1, clubStandings, 0)

  predictions <- data_input[,-1]

  bonus <- -50*(predictions[topClub,]==1)
  ssq <- function(x){(x-clubStandings)^2}
  squares <- apply(predictions,2,ssq)
  score <- colSums(squares) + bonus
  names <- colnames(score)
  names1 <- stringr::str_replace_all(names, "_", " ")
  worst <- apply(squares,2,max)
  findWorst <- function(y){match(worst[y],squares[,y],0)}
  worstClubNo <- sapply(1:ncol(predictions),findWorst)
  worstClub <- clubsABC[worstClubNo]

  output <- rbind(score, bonus, worst, worstClubNo)
  output1 <- data.frame(names1,t(output))
  row.names(output1) <- NULL
  output2 <- output1 %>%
    dplyr::mutate(worstClub=clubsABC[worstClubNo]) %>%
    dplyr::select(-worstClubNo)
  colnames(output2) <- c("Names", "Scores", "Bonus", "WorstCost", "WorstClub")

  report <- output2 %>%
    dplyr::arrange(Scores) %>%
    dplyr::select(Names, Scores, Bonus, WorstClub, WorstCost)

  print(report)

}
p0bs/premPredictor documentation built on April 23, 2020, 2 p.m.