--- title: "City Compare" output: html_document: toc: true toc_depth: 5 toc_float: collapsed: false smooth_scroll: true editor_options: chunk_output_type: console --- # Input Data & Configuration ## Libraries ```{r libs, eval = TRUE, echo = TRUE, results = "show", warning = FALSE, error = TRUE, message = FALSE} date() rm(list=ls()) library(tidyverse) library(tidycensus) ``` ## API keys ```{r api_keys, eval = TRUE, echo = TRUE, results = "show", warning = FALSE, error = TRUE, message = FALSE} # load census api key census_api_key(key = substr(read_file(file = "api_keys/census_api_key"), 1, 40)) ``` ## Cities to compare ```{r cities, eval = TRUE, echo = TRUE, results = "show", warning = FALSE, error = TRUE, message = FALSE} cities <- read_csv(file = "cities.csv") cities <- cities %>% mutate(city_name = paste0(City, " ", Type)) ``` # Get data ## Census data ```{r census, eval = TRUE, echo = TRUE, results = "show", warning = FALSE, error = TRUE, message = FALSE} populations <- list(NULL) for(city in cities$city_name){ state <- cities %>% filter(city_name == city) %>% pull(State) populations[[city]] <- get_acs( geography = "place", variables = "B01003_001", state = state, year = 2023 ) %>% filter(str_detect(NAME, city)) } populations <- bind_rows(populations) cities <- bind_cols(cities, populations) ggplot(cities) + geom_col(aes(x = City, y = estimate)) ``` ## Weather ```{r weather, eval = TRUE, echo = TRUE, results = "show", warning = FALSE, error = TRUE, message = FALSE} populations <- list(NULL) for(city in cities$city_name){ state <- cities %>% filter(city_name == city) %>% pull(State) populations[[city]] <- get_acs( geography = "place", variables = "B01003_001", state = state, year = 2023 ) %>% filter(str_detect(NAME, city)) } populations <- bind_rows(populations) cities <- bind_cols(cities, populations) ggplot(cities) + geom_col(aes(x = City, y = estimate)) ```