Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.