Last active
August 14, 2016 11:13
-
-
Save JoFrhwld/151259f073c6ad9e34c3bdb9e18f02b0 to your computer and use it in GitHub Desktop.
The most terrifying gist ever created.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' 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