Last active
October 22, 2016 15:10
-
-
Save ellisvalentiner/5ea4aa508a5c2638c298af98fbac1ed2 to your computer and use it in GitHub Desktop.
This file contains 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
library(rvest) | |
library(dplyr) | |
library(tidyr) | |
library(readr) | |
library(jsonlite) | |
library(lubridate) | |
### Begin scrape ### | |
polls <- xml2::read_html("http://projects.fivethirtyeight.com/2016-election-forecast/national-polls/") %>% | |
rvest::html_nodes(xpath = "//script[contains(., 'race.model')]") %>% | |
gsub(".*race.stateData = (.*);race.pathPrefix.*", "\\1", .) %>% | |
jsonlite::fromJSON(., flatten = TRUE) %>% | |
magrittr::extract2("polls") %>% | |
tidyr::unnest(votingAnswers) %>% | |
readr::type_convert() | |
### End scrape ### | |
### Example spread ### | |
polls %>% | |
select(id, population, sampleSize, state, startDate, endDate, choice, pct) %>% | |
tidyr::spread(choice, pct) %>% | |
arrange(desc(endDate)) %>% | |
slice(1:10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment