Skip to content

Instantly share code, notes, and snippets.

@seanjtaylor
Created November 6, 2020 06:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seanjtaylor/cd85175055e66cdc2bb7899a3bcdf313 to your computer and use it in GitHub Desktop.
Save seanjtaylor/cd85175055e66cdc2bb7899a3bcdf313 to your computer and use it in GitHub Desktop.
Benford's Law
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')
@seanjtaylor
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment