R/utils.R

Defines functions append_colnames guarantee_colnames most_common_year most_common_state replace_with_zero link_to_congbio coerce_if

append_colnames <- function(data, append) {
  stopifnot(is.data.frame(data))
  replacements <- paste0(colnames(data), append)
  # Only fix the data columns, not the state or county columns
  tofix <- colnames(data) %in% c("state", "county_ahcb", "county_fips")
  replacements[tofix] <- colnames(data)[tofix]
  colnames(data) <- tolower(replacements)
  data
}

guarantee_colnames <- function(data, parties) {
  parties <- tolower(unique(c(parties, "Other")))
  for (party in parties) {
    if (!any(paste0(party, "_vote") %in% colnames(data))) {
      data[[paste0(party, "_vote")]] <- 0
      data[[paste0(party, "_percentage")]] <- 0
    }
  }
  data
}

most_common_year <- function(years) {
  as.integer(names(sort(table(stats::na.omit(years)), decreasing = TRUE))[1])
}

most_common_state <- function(states) {
  out <- names(sort(table(stats::na.omit(states)), decreasing = TRUE))[1]
  out <- ifelse(out == "Vermont Republic", "Vermont", out)
  out
}

replace_with_zero <- function(x) { ifelse(is.na(x), 0, x) }

link_to_congbio <- function(text, url) {
  out <- paste0("<a href='", url, "'>", text, "</a>")
  out[is.na(url)] <- text[is.na(url)]
  out
}

coerce_if <- function(x) {
  out <- suppressWarnings(as.integer(x))
  if (any(is.na(out))) {
    return(x)
  } else {
    return(out)
  }
}
mapping-elections/mappingelections documentation built on July 24, 2019, 9:42 a.m.