Created
November 6, 2020 06:46
-
-
Save seanjtaylor/cd85175055e66cdc2bb7899a3bcdf313 to your computer and use it in GitHub Desktop.
Benford's Law
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(rvest) | |
doc <- read_html("https://county.milwaukee.gov/EN/County-Clerk/Off-Nav/Election-Results/Election-Results-Fall-2020") | |
doc %>% | |
html_nodes('table') %>% | |
.[[4]] %>% | |
html_table() %>% | |
tail(-1) %>% | |
mutate(ward = 1:n(), | |
biden = stringr::str_sub(X3, 1, 1), | |
trump = stringr::str_sub(X4, 1, 1) | |
) %>% | |
select(ward, biden, trump) %>% | |
gather(candidate, digit, -ward) %>% | |
group_by(candidate, digit) %>% | |
count() %>% | |
filter(digit > 0) %>% | |
group_by(candidate) %>% | |
mutate(prop = n / sum(n)) %>% | |
ggplot(aes(x = digit, y = prop, color = candidate, group = candidate)) + | |
geom_line() + | |
stat_function(fun = function(x) log10(1 + 1/x), color = 'black', linetype='dashed') |
Author
seanjtaylor
commented
Nov 6, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment