Skip to content
Snippets Groups Projects
data_utils.R 1.61 KiB
Newer Older
Azadpour, Elmera's avatar
Azadpour, Elmera committed
#' @title get census data of interest
#' @description pull census data of interest with set geography, variable of interest, and year. 
#' @param geography the geography of your data
#' @param variable Character string or vector of character strings of variable IDs. tidycensus automatically returns the estimate and the margin of error associated with the variable.
#' @param states, An optional vector of states for which you are requesting data. State names, postal codes, and FIPS codes are accepted. Defaults to NULL.
#' @param year The year, or endyear, of the ACS sample. 5-year ACS data is available from 2009 through 2021; 1-year ACS data is available from 2005 through 2021, with the exception of 2020. Defaults to 2021.
#' @param proj Set projection
#' @return a dataframe with census data 
get_census_data <- function(geography, variable, states, year, proj) {
  # Your code for fetching ACS data and processing it
  df <- get_acs(
    geography = geography,
    variable = variable,
    state = states,
    year = year,
    geometry = TRUE
  ) |>
    janitor::clean_names() |>
    st_transform(proj)
  return(df)
}

#' @title Join total population data with total_[variable] data to get percents of X population
#' @param tot_pop df of total population by county in conus
#' @param tot_var sf of estimated totals of X population by county in conus
#' @return a dataframe with additional `percent` column  
process_perc <- function(tot_pop, tot_var){
  
  joined_df <- left_join(tot_var, tot_pop, by = "geoid")
  
  joined_perc_df <- joined_df |> 
    mutate(percent = (as.numeric(estimate) / tot_pop) * 100)
  
  return(joined_perc_df)
}