Skip to content

Instantly share code, notes, and snippets.

@jonnylaw
Created October 9, 2017 12:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonnylaw/e882d245188a4b7c38425cc5e0eddbd6 to your computer and use it in GitHub Desktop.
Save jonnylaw/e882d245188a4b7c38425cc5e0eddbd6 to your computer and use it in GitHub Desktop.
library(tidyverse)
library(htmltab)
raw_results = htmltab::htmltab(doc = "http://www.harrierleague.com/results/2017-18/druridge/SenF.htm")
clean_results = raw_results %>%
as_data_frame() %>%
rename(race_time = `Race Time`, actual_time = `Actual Time`) %>%
filter(Cat != "guest") %>%
mutate(division = as.numeric(stringr::str_sub(Club, start = 2, end = 2)),
club = stringr::str_sub(Club, start = 5)) %>%
select(Name, actual_time, division, club) %>%
mutate(actual_seconds = as.numeric(lubridate::ms(actual_time)))
# Concatenation helper function
concat <- function(strings) {
Reduce(f = function(string_1, string_2) paste(string_1, string_2, sep = ", "), x = strings)
}
get_results_one_division = function(raw_results, counters = 4) {
incomplete_teams = raw_results %>%
count(club) %>%
filter(n < counters)
raw_results %>%
anti_join(incomplete_teams, by = "club") %>%
group_by(club) %>%
top_n(n = counters, wt = desc(pos)) %>%
group_by(club) %>%
summarise(total_points = sum(pos), total_counters = n(), counters = concat(paste0(Name, " (", pos, ")"))) %>%
mutate(position = min_rank(total_points))
}
actual_time_results = clean_results %>%
group_by(division) %>%
mutate(pos = min_rank(actual_seconds)) %>%
do(get_results_one_division(.)) %>%
arrange(division, position)
actual_time_results %>%
filter(division == 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment