jalapic / engsoccerdata

English and European soccer results 1871-2022
755 stars 192 forks source link

Helper function to manually add latest results to dataframe #44

Closed JoGall closed 3 years ago

JoGall commented 6 years ago

Just posting a helper function for manually adding new results to the results dataframe in case you want to include it in the package. I often find myself wanting to analyse the latest results before the football-data.co.uk CSVs have been updated so this comes in handy. It uses the matchTeamnames() function described here.

addResult <- function(home, visitor, hgoal, vgoal, Date = Sys.Date(), division = 1, tier = 1, Season = 2017) {

  home <- matchTeamnames(home)
  visitor <- matchTeamnames(visitor)

  FT <- paste0(hgoal, "-", vgoal)
  totgoal <- hgoal + vgoal
  goaldif <- hgoal - vgoal
  result <- ifelse(hgoal > vgoal, "H", ifelse(hgoal < vgoal, "A", "D"))

  df <- data.frame(Date, Season, home, visitor, FT, hgoal, vgoal, division, tier, totgoal, goaldif, result)

  rbind(england, df) %>% 
    arrange(Date)
}

Example usage:

addResult("Man City", "Stoke", 7, 2, Sys.Date() - 1) %>% 
  tail(2)

#             Date Season             home    visitor  FT hgoal vgoal division tier totgoal goaldif result
#194110 2017-10-01   2017 Newcastle United  Liverpool 1-1     1     1        1    1       2       0      D
#194111 2017-10-14   2017  Manchester City Stoke City 7-2     7     2        1    1       9       5      H
jalapic commented 3 years ago

using the _current() functions to basically do this.