Aggregating Mercer Island local election results

R
local
Published

December 11, 2016

National and local news outlets report the results of the Presidential election at the state and county level, but that doesn’t tell me how my neighborhood voted. For that I’ll need the precinct-by-precinct results, which in my area is not reported. Can I calculate it myself? In R the answer is of course a big YES. Here’s how I did it.

First you obtain the full King County election results as a precinct-by-precint CSV file.

Filter the results to look for just the Mercer Island precincts, and further narrow down to just the US Presidential results.

Code
#election2016<-read.csv("../../data/November_2016_ECanvass.csv")
election2016<-read.csv(file.path(Sys.getenv("BLOGDIR"),"content/post/data/November_2016_ECanvass.csv"))
mercerIsland2016<- election2016 %>% dplyr::filter(grepl("M-I",as.character(Precinct)))
mi.president.all<-dplyr::filter(mercerIsland2016,Race=="US President & Vice President")   %>% 
  dplyr::select(precinct=Precinct,candidate=CounterType,sum=SumOfCount)

#mi.president.all %>% group_by(candidate) %>% summarize(sum=sum(sum)) %>% arrange(desc(sum))

mi.president.all %>% dplyr::filter(candidate!="Times Counted" &
                              candidate!="Registered Voters") %>%
  dplyr::group_by(candidate) %>% 
  dplyr::summarize(sum=sum(sum)) %>%
  dplyr::arrange(desc(sum)) %>%
  dplyr::mutate(pct=sum/sum(sum)*100) %>%
  knitr::kable(digits=3)

Top Candidates

Sometimes it’s nice to compare just among votes case for the “top candidates”, like this:

Code
topCandidates<-c("Donald J. Trump & Michael R. Pence",
                 "Gary Johnson & Bill Weld",
                 "Jill Stein & Ajamu Baraka",
                 "Hillary Clinton & Tim Kaine")

mi.president<-mi.president.all %>% dplyr::filter(candidate %in% topCandidates)
mi.president.all$candidate<-factor(mi.president$candidate)

mi.president.top<-mi.president %>% dplyr::group_by(candidate) %>%
  dplyr::summarize(sum=sum(sum)) %>%
  dplyr::arrange(desc(sum)) %>%
  dplyr::mutate(pct=sum/sum(sum)*100)

knitr::kable(mi.president.top,digits=3)

and that’s it!