Created
November 6, 2020 01:57
-
-
Save HenrikBengtsson/2d210aae0711e47a4ed23a24d615de3e to your computer and use it in GitHub Desktop.
This file contains 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
read_nyt_vote_table <- function(state, force = FALSE) { | |
library(rvest) ## CRAN package 'rvest' | |
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