-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelection_results_florida.R
67 lines (55 loc) · 1.56 KB
/
election_results_florida.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
###################################
# Clean Florida election results precinct level
# Raw data is from
# https://dos.myflorida.com/elections/data-statistics/elections-data/precinct-level-election-results/
###################################
# Packages used
library(tidyverse)
library(data.table)
library(rvest)
# Directory containing raw files
# Each election is broken down into seperate folders
# for example, 2016 Primary or 2016 General
f_pri_dir <- 'data'
# List of file names
filez <- dir(f_pri_dir, full.names = TRUE)
# Data columns
p_level_cols <- c(
'county_code',
'county',
'election_number',
'date',
'election_name',
'primary_id',
'precint_location',
'tot_reg_voters',
'tot_reg_reps',
'tot_reg_dems',
'tot_reg_other',
'contest_name',
'district',
'contest_code',
'candidate_or_issue',
'party',
'candidate_id',
'doe',
'vote_totals'
)
# go through files and read each one using data.table fread
fl_election_results <- filez %>%
map( ~ fread(.x, col.names = p_level_cols, quote = "") %>%
as_tibble())
fl_election_results
# set columns as character to prevent load errors
fl_election_results <- fl_election_results %>%
map(~mutate(.x,
primary_id = as.character(primary_id),
precint_location = as.character(precint_location),
vote_totals = as.character(vote_totals))) %>%
bind_rows()
# convert vote totals back integer
fl_election_results <- fl_election_results %>%
mutate(vote_totals = as.integer(vote_totals))
# export data
fl_election_results %>%
write_csv('results.csv')