more info at http://timthoughts.com/2016/04/16/Election_2/
scraping from the Green Papers
library(rvest)
al<- "http://www.thegreenpapers.com/P16/AL-R"
al<- read_html(al)
al<-html_table(html_nodes(al, "table"), fill=TRUE)[7]
al<-data.frame(al)
al<-al[c(-1:-2, -12),]
al[2:13]<-apply(al[2:13], 2, function(x) as.numeric(gsub(",", "", x)))
names(al)<- c("CD","Pop_Vote","Qual_Vote", "Total_Del","Trump_Pop","Trump_Alloc","Trump_Del","Cruz_Pop","Cruz_Alloc","Cruz_Del","Rubio_Pop","Rubio_Alloc","Rubio_Del")
scraping an Excel url
library(gdata)
library(tools)
library(stringr)
library(dplyr)
url<-"http://www.alabamavotes.gov/downloads/election/2016/primary/primaryResultsCertified-Republican-Spreadsheet-2016-03-11.xlsx"
alabama_by_congress <- read.xls(url, na="")
alabama_by_congress<- alabama_by_congress[1:3]
alabama_by_congress<- na.omit(alabama_by_congress[74:183,])
names(alabama_by_congress)<- c("Candidate", "Votes", "Percent")
alabama_by_congress$State<- "Alabama"
alabama_by_congress$Votes<-as.numeric(gsub(",","",alabama_by_congress$Votes))
alabama_by_congress$Candidate<- toTitleCase(word(tolower(alabama_by_congress$Candidate),-1))
alabama_by_congress<- alabama_by_congress%>% group_by(Candidate) %>% slice(1:7) %>% filter(Candidate !="Total_cd", Votes != "Votes")
alabama_by_congress$Percent<-as.numeric(sub("%","",alabama_by_congress$Percent))
alabama_by_congress$District<- paste0(str_pad(1:7, 2, "left", pad="0"))
alabama_by_congress<- alabama_by_congress[,c(4,5,1,2,3)]