Skip to content

Instantly share code, notes, and snippets.

@JoFrhwld
Last active August 14, 2016 11:13
Show Gist options
  • Save JoFrhwld/151259f073c6ad9e34c3bdb9e18f02b0 to your computer and use it in GitHub Desktop.
Save JoFrhwld/151259f073c6ad9e34c3bdb9e18f02b0 to your computer and use it in GitHub Desktop.
The most terrifying gist ever created.
#' rvest for scraping 538
library(rvest)
library(magrittr)
#' scrape the forecast
five38 <- read_html("http://projects.fivethirtyeight.com/2016-election-forecast/?ex_cid=rrpromo#plus")
#' I'd prefer to be using the polls-pluss forecast here, but
#' can only seem to get the polls only
clinton <- five38 %>%
html_node(xpath = '//*[contains(concat( " ", @class, " " ),
concat( " ", "dem", " " ))]
//*[contains(concat( " ", @class, " " ),
concat( " ", "winprob", " " ))]')%>%
html_text()%>%
gsub("%", "", .)%>%
as.numeric()%>%
divide_by(100)
trump <- five38 %>%
html_node(xpath = '//*[contains(concat( " ", @class, " " ),
concat( " ", "rep", " " ))]
//*[contains(concat( " ", @class, " " ),
concat( " ", "winprob", " " ))]')%>%
html_text()%>%
gsub("%", "", .)%>%
as.numeric()%>%
divide_by(100)
#' "The 45th President of the United States is..."
sample(c("Hillary Clinton", "Donald Trump"), size = 1, prob = c(clinton, trump))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment