Skip to content

Instantly share code, notes, and snippets.

@anbnyc
Created August 29, 2017 13:01
Show Gist options
  • Save anbnyc/c92aefb621abf79851fe4f620841285b to your computer and use it in GitHub Desktop.
Save anbnyc/c92aefb621abf79851fe4f620841285b to your computer and use it in GitHub Desktop.
clean voter file
voters <- read.csv("~/Downloads/voter_file.csv", stringsAsFactors = F)
voters_sub <- voters[c("SBOEID","DOB","REGDATE","ENROLLMENT","LASTVOTEDATE","COUNTYCODE","ED","WARD","SD","AD","LD")]
existing_eds <- tbl_df(voters_sub) %>%
group_by(ED, ENROLLMENT) %>%
summarise(count = n()) %>%
spread(ENROLLMENT, count) %>%
mutate(TOTAL = sum(BLK,CON,DEM,GRE,IND,OTH,REF,REP,WEP,WOR,na.rm=T),
DEM_PCT = DEM/TOTAL)
new_dems_eds <- tbl_df(voters_sub) %>%
filter(REGDATE >= 20161109) %>%
filter(is.na(LASTVOTEDATE)) %>%
group_by(ED, ENROLLMENT) %>%
summarise(count = n()) %>%
spread(ENROLLMENT, count) %>%
mutate(NEW_TOTAL = sum(BLK,CON,DEM,GRE,IND,OTH,REF,REP,WEP,WOR,na.rm=T),
NEW_DEM=DEM,
NEW_DEM_PCT = NEW_DEM/NEW_TOTAL)
eds <- merge(existing_eds[,c("ED","DEM","DEM_PCT","TOTAL")],
new_dems_eds[,c("ED","NEW_DEM","NEW_DEM_PCT","NEW_TOTAL")],
by="ED")
write.csv(eds, "~/Downloads/brookhaven_eds.csv")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment