Skip to content

Instantly share code, notes, and snippets.

@jonrobinson2
Created February 5, 2018 20:23
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 jonrobinson2/2cb9015c115ac0b1ee3cf20c627c9a4c to your computer and use it in GitHub Desktop.
Save jonrobinson2/2cb9015c115ac0b1ee3cf20c627c9a4c to your computer and use it in GitHub Desktop.
require(data.table)
require(sqldf)
require(rvest)
require(dplyr)
fairvote=read_html('https://infogram.com/cong-primaries-by-party-1g0n2owgxvyrp4y') %>%
html_nodes('script') %>%
html_text()
fairvote=fairvote[[7]]
fairvote=unlist(strsplit(x = fairvote,fixed = T, split = c('[\"','\"]')))
fairvote=fairvote[max(grep(pattern = c('Semi-Closed'),x = fairvote)):max(grep(pattern = c('Wyoming'),x = fairvote))]
fairvote=data.frame(bind_rows(lapply(2:length(fairvote),function(x){
parsed=unlist(strsplit(x = fairvote[x],split = '",'))[1:7]
parsed=gsub("[^A-Z a-z 0-9]","", parsed)
parsed=matrix(parsed, ncol=7, byrow=TRUE)
parsed=as.data.frame(parsed, stringsAsFactors=FALSE)
names(parsed)=gsub("[^A-Z a-z 0-9]","",x=unlist(strsplit(x = fairvote[1],split = ','))[1:7])
return(parsed)
})))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment