Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Texas Historical Data 2008 - 2016 #41

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 30 additions & 17 deletions r-packages/uselections/R/tx.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,39 @@
#' @importFrom tibble as_tibble
#' @export
loadTexas <- function() {

#must set working directory to "election-transparency/r-packages/uselections"
stopifnot(basename(getwd()) == "uselections")

countyNameFIPSMapping <- getCountyNameFIPSMapping('48') %>%
mutate(CountyName=toupper(CountyName))

page <- read_html("https://www.sos.state.tx.us/elections/historical/nov2016.shtml")
tables <- page %>% html_nodes("table") %>% html_table(fill=TRUE)

df <- tables[[1]] %>%
filter(`County Name` != 'Statewide Total') %>%
select(CountyName=`County Name`, N=`Voter Registration`) %>%
mutate_each(funs(gsub(x=., pattern=",", replacement=""))) %>%
mutate(D=NA, G=NA, L=NA, R=NA, O=NA) %>%
mutate(Year = 2016, Month = 11) %>% # Hardcode until we add historical data
select(CountyName, D, G, L, R, N, O, Year, Month) %>%
mutate_each("as.integer", -CountyName) %>%
mutate(CountyName=ifelse(CountyName=='LASALLE', 'LA SALLE', CountyName)) %>%
left_join(countyNameFIPSMapping, by=c("CountyName"="CountyName")) %>%
select(-CountyName) %>%
as_tibble()

df.list = list()
df.iterator <- 1
#start at 2008
for(i in seq(2008, 2016, 2))
{
page <- read_html(paste("https://www.sos.state.tx.us/elections/historical/nov", i, ".shtml", sep=""))
tables <- page %>% html_nodes("table") %>% html_table(fill=TRUE)

df.year.i <- tables[[1]] %>%
filter(`County Name` != 'Statewide Total') %>%
select(CountyName=`County Name`, N=`Voter Registration`) %>%
mutate_each(funs(gsub(x=., pattern=",", replacement=""))) %>%
mutate(D=NA, G=NA, L=NA, R=NA, O=NA) %>%
mutate(Year = i, Month = 11) %>% # Hardcode until we add historical data
select(CountyName, D, G, L, R, N, O, Year, Month) %>%
mutate_each("as.integer", -CountyName) %>%
mutate(CountyName=ifelse(CountyName=='LASALLE', 'LA SALLE', CountyName)) %>%
left_join(countyNameFIPSMapping, by=c("CountyName"="CountyName")) %>%
select(-CountyName) %>%
as_tibble()

df.list[[df.iterator]] <- df.year.i
df.iterator <- df.iterator + 1
}

df <- do.call(rbind, df.list)

df

}