R/all_geo_table.R

#' Make ACS table for several geos
#'
#' Makes an ACS table for state, regions, counties, and towns
#'
#' @param town_lookup A data.frame or tbl. Must have columns \code{town} and \code{county}, as generated by \code{\link{get_town_names}}
#' @param table.number String corresponding to an ACS table number; this is case-sensitive
#' @param year Endyear of ACS estimates as a four-digit integer
#' @param regions A named list of character vectors with names of towns for each region
#' @param state Either a number corresponding to the state's FIPS code, or a string with the state's two-letter abbreviation or full name
#'
#' @return Returns an \code{\link[acs]{acs-class}} object, as is standard from the \code{acs} package.
#' @examples
#' ct_towns <- get_town_names(state = 9)
#' regions <- list(
#'   "Greater New Haven" = c("Hamden", "East Haven", "West Haven", "Milford",
#'     "Orange", "Bethany", "Woodbridge", "North Haven", "Branford",
#'     "North Branford", "Madison", "Guilford", "New Haven"),
#'   "Inner Ring" = c("Hamden", "East Haven", "West Haven"),
#'   "Outer Ring" = c("Milford", "Orange", "Bethany", "Woodbridge",
#'     "North Haven", "Branford", "North Branford", "Madison", "Guilford")
#' )
#' populations <- all_geo_table(ct_towns, "B01003", 2015, regions = regions,
#'   state = 9)
#'
#' @seealso \code{\link{make_regional_geo}}, called by this function, and \code{\link{get_town_names}}, which returns a dataframe appropriate for \code{town_lookup}
#'
#' @export
all_geo_table <- function(town_lookup, table.number, year = 2015, regions, state = 9) {
  if(!"county" %in% names(town_lookup)) stop("town_lookup missing column county. Try again with function get_town_names")
  if(!"town" %in% names(town_lookup)) stop("town_lookup missing column town. Try again with function get_town_names")

  state_geo <- acs::geo.make(state = state)
  region <- purrr::map2(regions, names(regions), ~make_regional_geo(.x, .y, town_lookup = town_lookup)) %>% purrr::reduce(c)
  county <- acs::geo.make(state = state, county = "*")
  towns <- acs::geo.make(state = state, county = "*", county.subdivision = "*")
  geos <- c(state_geo, region, county, towns)
  table <- acs::acs.fetch(geography = geos, endyear = year, table.number = table.number, col.names = "pretty")
  return(table)
}
CT-Data-Haven/acsprofiles documentation built on June 13, 2019, 8:20 a.m.