Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pres_results data off #8

Open
brshallo opened this issue Sep 11, 2020 · 2 comments
Open

pres_results data off #8

brshallo opened this issue Sep 11, 2020 · 2 comments

Comments

@brshallo
Copy link

Results look different than expected. For example https://en.wikipedia.org/wiki/2016_United_States_presidential_election_in_Washington_(state) says 2016 election had 54.3% of vote for Clinton and 38.07% for Trump. Or perhaps I'm misunderstanding specifics of data. Also the total for dem, rep, and other does not add-up to 1.

library(tidyverse)

politicaldata::pres_results %>% 
  filter(year == 2016, state == "WA")
#>   year state total_votes       dem       rep      other
#> 1 2016    WA     3317019 0.5253868 0.3683268 0.07378583

Created on 2020-09-10 by the reprex package (v0.3.0)

@brshallo
Copy link
Author

Looked into a little closer and checked data source, and difference is caused by whether write-in votes are counted or not.

@brshallo
Copy link
Author

brshallo commented Sep 11, 2020

To get a similar output to yours but with vote proportions that come to zero I installed .csv data from data source and then ran:

library(tidyverse)

vote_raw <- read_csv("1976-2016-president.csv")

votes_clean <- vote_raw %>% 
  mutate(party = ifelse(is.na(party), "", party)) %>% 
  mutate(party = case_when(party == "democrat" ~ "dem",
                           party == "republican" ~ "rep",
                           TRUE ~ "other")) %>% 
  # filter(!writein) %>% # include if not wanting to include write-ins
  select(-state) %>% 
  rename(state = state_po) %>% 
  group_by(year, state, party) %>% 
  summarise(votes = sum(candidatevotes)) %>% 
  ungroup() %>% 
  spread(party, votes) %>% 
  relocate(other, .after = last_col()) %>% 
  mutate(across(c(dem, rep, other), ~ ifelse(is.na(.), 0, .))) %>% 
  mutate(total_votes = dem + rep + other) %>% 
  mutate(across(c(dem, rep, other), ~ . / total_votes))

See brshallo/weighted-national-popular-vote.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant