Skip to content

Instantly share code, notes, and snippets.

@vmburbinamx
Created June 17, 2018 14:44
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 vmburbinamx/164f296cc3c95e1a6f1865d78736a2f7 to your computer and use it in GitHub Desktop.
Save vmburbinamx/164f296cc3c95e1a6f1865d78736a2f7 to your computer and use it in GitHub Desktop.
The world cup winner 2018
library(stringr)
library(tidyr)
library(dplyr)
library(ggplot2)
library(ggthemes)
#Load function
source("getDataFromOddsChecker.R")
#Load World Cup Winner table
worldCupWinner <- oddschecker("football/world-cup/winner")
#Set row name as an additional column
worldCupWinner <- add_rownames(worldCupWinner)
names(worldCupWinner)[1] <- "Country"
#Transpose data
worldCupWinner <- worldCupWinner %>% gather("Source", "Bet", 2:ncol(worldCupWinner))
#remove empty values
worldCupWinner <- worldCupWinner %>% filter(Bet != "")
#Get first element into a separate column
numbersInSeparateColumns <- str_split(worldCupWinner$Bet, "/")
firstElement <- c()
for (listItem in seq_along(numbersInSeparateColumns)) {
listItemValue <- numbersInSeparateColumns[[listItem]][[1]]
firstElement <- c(firstElement, listItemValue)
}
firstElement <- as.numeric(firstElement)
firstElement <- as_data_frame(firstElement)
worldCupWinner <- bind_cols(worldCupWinner, firstElement)
names(worldCupWinner)[4] <- "First"
remove(firstElement, listItem, listItemValue)
#Get second element into a separate column
secondElement <- c()
for (listItemTwo in seq_along(numbersInSeparateColumns)) {
listItemValue <- numbersInSeparateColumns[[listItemTwo]][[2]]
if (listItemValue == "") {
listItemValue <- NA
}
secondElement <- c(secondElement,listItemValue)
}
secondElement <- as.numeric(secondElement)
secondElement <- as_data_frame(secondElement)
worldCupWinner <- bind_cols(worldCupWinner, secondElement)
names(worldCupWinner)[5] <- "Second"
remove(secondElement, listItemTwo, listItemValue, numbersInSeparateColumns)
#Get factor
worldCupWinner$Factor <- worldCupWinner$First/worldCupWinner$Second
#Create a chart
theGroup <- c("Mexico", "Germany")
theChartData <- worldCupWinner %>% filter(Country %in% theGroup)
theChart <- ggplot(theChartData, aes(x = Country, y = Factor, Color = Country)) +
geom_boxplot() +
scale_x_discrete() +
geom_jitter() +
ggtitle(paste("¿Qué dicen las apuestas? Última consulta:", Sys.time())) +
theme_economist()
print(theChart)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment