Skip to content

Commit

Permalink
Fix round number for Wednesday matches (#96)
Browse files Browse the repository at this point in the history
Fix round number for Wednesday matches
  • Loading branch information
jimmyday12 authored Dec 9, 2019
2 parents 45d7b0d + 49327eb commit 47bf232
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Addition of `replace_venues` - changes venue names for all data sources to match AFL Tables ([#15](https://github.com/jimmyday12/fitzRoy/issues/15), [@cfranklin11](https://github.com/cfranklin11))

## Bug Fixes
* Fixed incorrect round numbers for fixture and betting data from `footywire.com` ([#93](https://github.com/jimmyday12/fitzRoy/issues/93), [@cfranklin11](https://github.com/cfranklin11))
* Fixed incorrect round numbers for fixture and betting data from `footywire.com` ([#93](https://github.com/jimmyday12/fitzRoy/issues/93) & [#95](https://github.com/jimmyday12/fitzRoy/issues/95), [@cfranklin11](https://github.com/cfranklin11))

# fitzRoy 0.2.0
This release is in preparation for a CRAN submission. There are some breaking changes and removal of early functions that are no longer supported.
Expand Down
13 changes: 7 additions & 6 deletions R/footywire-calcs.R
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ update_footywire_stats <- function(check_existing = TRUE) {
#' Helper function for \code{get_fixture,betting_data}
#'
#' Work out round number of each game from day and week.
#' Games from Thursday through Wednesday go in same Round.
#' Games from Wednesday through Tuesday go in same Round.
#'
#' @param data_frame A data frame with match-level data and a Date column
#' @importFrom magrittr %>%
Expand Down Expand Up @@ -162,14 +162,15 @@ calculate_round <- function(data_frame) {
dplyr::select(-c(.data$round_diff,.data$cumulative_diff))
}

# Special cases where week counting doesn't work
fix_incorrect_rounds <- function(incorrect_df) {
round_df <- data.frame(incorrect_df)

# Special cases where week counting doesn't work: 2018 collingwood/essendon
# 2018 Collingwood/Essendon: Unlike default, this Wednesday match belongs
# to previous round (i.e. it's the end of the round, not the beginning)
round_five <- 5
# Need to use date for filter, because betting data doesn't include time
round_indices_to_fix <-
lubridate::date(round_df$Date) == "2018-04-25"
round_indices_to_fix <- lubridate::date(round_df$Date) == "2018-04-25"
round_df$Round[round_indices_to_fix] <- round_five

# 2012-2014: first round shifts round numbers for rest of season
Expand All @@ -185,15 +186,15 @@ calculate_round <- function(data_frame) {

calculate_round_by_week <- function(roundless_df) {
sunday <- 1
wednesday <- 4
tuesday <- 3

round_df <- roundless_df %>%
dplyr::mutate(
Season = lubridate::year(.data$Date),
week_count = lubridate::epiweek(.data$Date),
day_of_week = lubridate::wday(.data$Date),
Round = ifelse(
dplyr::between(.data$day_of_week, sunday, wednesday),
dplyr::between(.data$day_of_week, sunday, tuesday),
.data$week_count - 1,
.data$week_count
)
Expand Down
17 changes: 13 additions & 4 deletions tests/testthat/test-footywire.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ test_that("round 5, 2018 is calculated correctly for betting data", {
expect_equal(nrow(round_5_data), 9)
})

test_that("Wednesday matches are at start of round by default", {
testthat::skip_on_cran()
max_matches_per_round <- 9
betting_data <- get_footywire_betting_odds(2019, 2019)
# Round 6, 2019 has a Wednesday match
round_6_data <- betting_data %>% dplyr::filter(Round == 6)
expect_equal(nrow(round_6_data), 9)
})

test_that("minimum rounds are calculated per season", {
testthat::skip_on_cran()
# 2014 season starts in week 11, most others start in week 12
Expand All @@ -102,14 +111,14 @@ test_that("minimum rounds are calculated per season", {
expect_equal(min(betting_data_2015$Round), 1)
})

test_that("round weeks are calculated from wednesday to monday", {
test_that("round weeks are calculated from Thursday to Wednesday", {
testthat::skip_on_cran()
betting_data <- get_footywire_betting_odds(2010, 2010)
round_1_data = betting_data %>% dplyr::filter(Round == 1)

# If epiweeks aren't adjusted properly (Sunday to Wednesday), the first round
# of 2010 will only have 5 matches due to 3 taking place on Sunday (the start
# of a new epiweek)
# If epiweeks aren't adjusted properly (Sunday to Wednesday belong
# to previous week), the first round of 2010 will only have 5 matches
# due to 3 taking place on Sunday (the start of a new epiweek)
expect_equal(nrow(round_1_data), 8)
})

Expand Down

0 comments on commit 47bf232

Please sign in to comment.