Forked from svmiller/scrape-tidy-presidency-project-approval-data.R
Created
May 27, 2018 18:03
-
-
Save briatte/d55f79b307ca54c2b43f8a97501ad42f to your computer and use it in GitHub Desktop.
Scrape American Presidency Project Approval Data
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
library(rvest) | |
library(lubridate) | |
library(tidyverse) | |
Truman <- read_html("http://www.presidency.ucsb.edu/data/popularity.php?pres=33") | |
Truman %>% | |
html_table(fill=T) -> Truman | |
Truman[[11]] -> Truman | |
Truman %>% | |
select(X1:X3, X5:X7) %>% | |
slice(7:n()) -> Truman | |
for (i in 34:45) { | |
url <- paste0("http://www.presidency.ucsb.edu/data/popularity.php?pres=",i) | |
url %>% | |
read_html() %>% | |
html_table(fill=TRUE) -> holdme | |
holdme[[11]] -> holdme | |
holdme %>% | |
select(X1:X3, X5:X7) %>% | |
slice(7:n()) -> holdme | |
Truman <- rbind(Truman, holdme) | |
} | |
Truman %>% | |
setNames(., c("potus", "stdate", "enddate", "approve", "disapprove", "unsure")) %>% | |
mutate(stdate = mdy(stdate), | |
enddate = mdy(enddate), | |
potus = ifelse(potus == "", NA, potus), | |
approve = as.integer(approve), | |
disapprove = as.integer(disapprove), | |
unsure = as.integer(unsure), | |
netapprove = approve - disapprove) %>% | |
tidyr::fill(potus) %>% arrange(stdate) -> Approval | |
write_csv(Approval, "potus-approval.csv") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment