Tuesday, March 22, 2016

Checking Caucuses

Some tricky, dense political code that reveals information about Precinct Sites based on Voting History.  Ask if you need the spreadsheet. Political post here. Click to enlarge.



Code after the break.


library(data.table)
library(dplyr)
library(lattice)
library(lubridate)

setwd("C:/Politics")
# set up variables and subsets
# voterdb012716 <- read.delim("C:/Politics/RyanFerrisList_20160127_105516/RyanFerrisList_20160127_105516_JessieS.txt", header = TRUE,fill=FALSE,strip.white = TRUE, sep = "\t", quote = "", stringsAsFactors = FALSE) 
# vdb <- voterdb012716 # not indexed

voterdb031616 <- read.delim("C:/Politics/RyanFerrisVoterList_20160316_112111_JessieS/RyanFerrisVoterList_20160316_112111_JessieS.txt", header = TRUE,fill=FALSE,strip.white = TRUE, sep = "\t", quote = "", stringsAsFactors = FALSE)
vdb <- voterdb031616

# voterdb012716 <- fread("C:/Politics/RyanFerrisList_20160127_105516/RyanFerrisList_20160127_105516_JessieS.txt")
# using fread instead of read.delim is faster but will require some change to the lubridate/as.Date code used below:
# voterdb012716$LastUpdateDate <- parse_date_time(voterdb012716$LastUpdateDate, "Ymd_hms", truncated = 4)
# vdb <- voterdb012716
# data.table(cbind(as.matrix(names(vdb)),as.matrix(sapply(vdb,class))))
# setkey(vdb,RegistrationNumber)
# vdb$LastUpdateDate <- as.Date(ymd_hms(voterdb012716$LastUpdateDate))
# vdb$BirthDate <- mdy(voterdb012716$BirthDate)

DT <- data.table(vdb)
setkey(DT,RegistrationNumber) # indexed!

DP <- read.csv("C:/Politics/Bernie/DemPrimary_002.csv")
data.table(count(DP,Caucus,sort=TRUE))
DTA <- DT[StatusCode == "A"]

setkey(DTA,RegistrationNumber)
colnames(DP) <- c("PrecinctID","Caucus","City","StateZIP")
PID <- data.table(xtabs(~ PrecinctID,data=DTA))
DP1 <- merge(DP,PID,by="PrecinctID")
colnames(DP1) <- c("PrecinctID","Caucus","City","StateZIP","Registered")

# DTA[BallotCounted_1 == 1]
BC1 <- data.table(xtabs(~ PrecinctID,data=DTA[BallotCounted_1 == 1]))
DP1 <- merge(DP1,BC1,by="PrecinctID",all=TRUE)
colnames(DP1) <- c("PrecinctID","Caucus","City","StateZIP","Registered","VotedPrimary2015")

BC2 <- data.table(xtabs(~ PrecinctID,data=DTA[BallotCounted_2 == 1]))
DP1 <- merge(DP1,BC2,by="PrecinctID",all=TRUE)
colnames(DP1) <- c("PrecinctID","Caucus","City","StateZIP","Registered","VotedPrimary2015","VotedPrimary2014")

BC4 <- data.table(xtabs(~ PrecinctID,data=DTA[BallotCounted_4 == 1]))
DP1 <- merge(DP1,BC4,by="PrecinctID",all=TRUE)
colnames(DP1) <- c("PrecinctID","Caucus","City","StateZIP","Registered","VotedPrimary2015","VotedPrimary2014","VotedPrimary2012")


DP1 <- data.table(DP1)
setkey(DP1,PrecinctID)
DP2 <- data.table(count(DP1,Caucus,sort=TRUE))

SR <- {};for(i in DP2$Caucus) {SR <- rbind(SR,(sum(DP1[Caucus == i,Registered])))};DP2 <- cbind(DP2,SR)
SR <- {};for(i in DP2$Caucus) {SR <- rbind(SR,(sum(DP1[Caucus == i,VotedPrimary2015],na.rm=TRUE)))};DP2 <- cbind(DP2,SR)
SR <- {};for(i in DP2$Caucus) {SR <- rbind(SR,(sum(DP1[Caucus == i,VotedPrimary2014],na.rm=TRUE)))};DP2 <- cbind(DP2,SR)
SR <- {};for(i in DP2$Caucus) {SR <- rbind(SR,(sum(DP1[Caucus == i,VotedPrimary2012],na.rm=TRUE)))};DP2 <- cbind(DP2,SR)

colnames(DP2) <- c("Caucus Location","Precincts","Registered","VotedPrimary2015","VotedPrimary2014","VotedPrimary2012")
setkey(DP2,"Caucus Location")
colSums(as.data.frame(DP2)[3:6],na.rm=TRUE)
arrange(DP2,desc(Precincts))

No comments:

Post a Comment