Skip to content

Instantly share code, notes, and snippets.

@HenrikBengtsson
Created November 6, 2020 01:55
Show Gist options
  • Save HenrikBengtsson/5d4b2185477128ab7df291be83f97e98 to your computer and use it in GitHub Desktop.
Save HenrikBengtsson/5d4b2185477128ab7df291be83f97e98 to your computer and use it in GitHub Desktop.
US Election 2020 Swing States
library(rvest) ## CRAN package 'rvest'
read_nyt_vote_table <- function(state, force = FALSE) {
states <- c(AZ = "arizona", GA = "georgia", NC = "north-carolina", NV = "nevada", PA = "pennsylvania")
stopifnot(state %in% names(states))
file <- sprintf("results-%s-president.html", states[state])
url <- file.path("https://www.nytimes.com/interactive/2020/11/03/us/elections", file)
if (force || !file_test("-f", file)) {
message("Downloading: ", file)
download.file(url, destfile = file, quiet = TRUE)
}
stopifnot(utils::file_test("-f", file))
id <- sprintf("%s-G-P-2020-11-03", state)
html <- read_html(file)
xpath <- sprintf('//*[@id="%s-results-table-container"]', id)
data <- html_node(html, xpath = xpath)
table <- html_children(data)[[2]]
table <- html_table(table, header = FALSE)
table <- table[, -1L]
colnames(table) <- c("candidate", "party", "votes", "fraction")
idx <- grep("View", table[,"candidate"])
if (length(idx) > 0) table <- table[-idx,]
table[,"candidate"] <- gsub("[*[:space:]].*", "", table[,"candidate"])
table[,"votes"] <- as.integer(gsub(",", "", table[,"votes"]))
table[,"fraction"] <- as.numeric(gsub("[<%]", "", table[,"fraction"]))/100
table <- table[, c("candidate", "votes", "fraction")]
table
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment