-
Notifications
You must be signed in to change notification settings - Fork 0
/
precinctData.R
39 lines (38 loc) · 1018 Bytes
/
precinctData.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#Precinct Data Aggregator
years <- c("2012", "2014", "2016")
prefixes <- list()
prefixes[[years[1]]] <- "/13-"
prefixes[[years[2]]] <- "/21-"
prefixes[[years[3]]] <- "/23-"
for (year in years)
{
precincts <- data.frame()
for (i in 1:55) {
p <-
read.csv(
paste("data/", year, prefixes[[year]], as.character(i), ".csv", sep = ""),
stringsAsFactors = FALSE
)
p$VoteTotal <- 0
for (i in 1:nrow(p))
{
p[i, 19] <-
sum(p[intersect(which(p[i, 12] == p[, 12]), which(p[i, 7] == p[, 7])), 18])
}
p$DistrictName <- toupper(p$DistrictName)
precincts <- rbind(precincts, p)
}
precincts$Percent <- round(precincts$Votes / precincts$VoteTotal, digits = 2)
write.csv(
x = precincts,
file = paste("precinctNumbers", year, ".csv", sep = ""),
row.names = FALSE,
na = ""
)
write.csv(
x = precincts[, c(6, 4, 7, 12, 13, 14, 15, 16)],
file = paste("precinctLabels", year, ".csv", sep = ""),
row.names = FALSE,
na = ""
)
}