forked from joyawood/Election-twitter-visualization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_prep.r
31 lines (21 loc) · 841 Bytes
/
data_prep.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
library(dplyr)
primaries <- read.csv("primary_results.csv")
state_names <-read.csv("state_names.csv")
state_names<- state_names %>%
select(name, abbreviation) %>%
rename(state = name)
by_candidate_and_state <- primaries %>%
group_by(candidate, state) %>%
summarize(sum(votes)) %>%
rename(votes_candidate = `sum(votes)`)
total_votes_state <- primaries %>%
group_by(state) %>%
summarise(sum(votes)) %>%
rename(votes_state = `sum(votes)`)
merged_data <- inner_join(by_candidate_and_state, total_votes_state, by = "state" )
merged_data <- merged_data %>%
mutate(prop = votes_candidate/votes_state)
merged_data <- merged_data %>%
filter(candidate!= " No Preference" & candidate!= " Uncommitted" )
merged_data <- left_join(merged_data, state_names)
write.csv(merged_data, file = "final_primary_data.csv")