Skip to content

Instantly share code, notes, and snippets.

@djhurio
Created July 30, 2017 14:12
Show Gist options
  • Save djhurio/ea24ade7c69ea58e2fe9a9ecd2288a49 to your computer and use it in GitHub Desktop.
Save djhurio/ea24ade7c69ea58e2fe9a9ecd2288a49 to your computer and use it in GitHub Desktop.
Grafiski attēlots ātrāko skrējēju laiks kontrolpunktos
# Stirnu buks
# 2017
# Pokaiņi
# Distance: Buks
require(rvest)
require(data.table)
require(ggplot2)
# HTML
html <- read_html("http://www.stirnubuks.lv/rezultati/spliti/?posms=pokaini&distance=buks")
# Table header
thead <- html %>% html_node("thead") %>% html_nodes("th") %>% html_text()
thead[thead == ""] <- paste0("V", 1:length(thead[thead == ""]))
thead <- gsub(" ", "", thead)
thead <- gsub("\\.", "", thead)
thead
# Table
dat <- html %>% html_node("table") %>% html_table(fill = T)
setDT(dat)
setnames(dat, thead)
names(dat)
dat[, .N]
# 466
# Izņem DNF
dat[Rezultāts == "DNF"]
dat <- dat[Rezultāts != "DNF"]
dat[, .N]
# 463
# Izņem dīvainus ierakstus ar tukšumiem
dat[is.na(V1) | is.na(V2) | is.na(V3) | is.na(V4) | is.na(V5) | is.na(V6)]
dat <- dat[!(is.na(V1) | is.na(V2) | is.na(V3) | is.na(V4) | is.na(V5) | is.na(V6))]
dat[, .N]
# 461
# Pārrēķina vietas
# Rezultātu skaits
N <- dat[, .N]
N
# CP skaits + finišs
n <- length(grep("^CP[1-9]", names(dat))) + 1L
n
# Starts
dat[, CP0 := "00:00:00"]
dat[, V0 := as.integer(NA)]
# Saglabā vietu
dat[, tmp := Vieta]
# Vieta dzimuma grupā
dat[, Vieta_dzim := as.integer(substring(Dzim, 2))]
# Dzimums
dat[, sex := substring(Dzim, 1, 1)]
setnames(dat, c("Rezultāts", "tmp"), paste0(c("CP", "V"), n))
for (i in 1:n) {
setorderv(dat, paste0(c("CP", "V"), i))
set(dat, j = paste0("V", i), value = 1:N)
}
dat
datl1 <- melt(dat, id.vars = c("Nr", "Dalībnieks", "sex", "Grupa",
"Vieta", "Vieta_dzim", "Dzim"),
measure.vars = paste0("CP", 0:n),
variable.name = "id", value.name = "CP")
datl1[, id := sub("CP|V", "", id)]
datl1
datl2 <- melt(dat, id.vars = "Nr", measure.vars = paste0("V", 0:n),
variable.name = "id", value.name = "V")
datl2[, id := sub("CP|V", "", id)]
datl2
datl <- merge(datl1, datl2, by = c("Nr", "id"))
setorder(datl, Vieta, id)
datl
datl[, class(CP)]
datl[, CP := as.POSIXct(CP, format = "%H:%M:%S")]
# datl[, pos := factor(Vieta, datl[order(-Vieta)][id == 0, Vieta],
# datl[order(-Vieta)][id == 0, paste(Vieta, Dalībnieks)])]
tmpval <- datl[order(-Vieta)][id == 0, Dzim]
tmplab <- datl[order(-Vieta)][id == 0, paste(Vieta_dzim, Dalībnieks, Grupa)]
datl[, pos := factor(Dzim, tmpval, tmplab)]
datl[Vieta < 11][order(pos)]
datl[sex == "V" & Vieta_dzim < 11][order(pos)]
datl[sex == "S" & Vieta_dzim < 11][order(pos)]
datl[, .N, keyby = Grupa]
plot_grupa <- function(dom = "VB") {
datp <- datl[grep(dom, Grupa)][1:min(.N, (n + 1L) * 15)]
pl <- ggplot(datp, aes(x = CP, y = pos, colour = pos)) +
geom_point() + geom_path() +
guides(colour = guide_legend(reverse = T)) +
ggtitle("Stirnu buks 2017 Poikaiņi", paste("Grupa:", dom)) +
xlab("Laiks") + ylab("") +
theme_bw()
return(pl)
}
plot_grupa("VB")
plot_grupa("SB")
tmp <- c("", "J", 2:4)
dom <- c(paste0("VB", tmp), paste0("SB", tmp))
nr <- sprintf("%02d", seq_along(dom))
plots <- lapply(dom, plot_grupa)
names(plots) <- paste0("SB2017-Pok-Buks-", nr, "-", dom, ".png")
names(plots)
for (f in names(plots)) {
ggsave(f, plot = plots[[f]], device = "png")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment