Library
library(here)
library(tidyverse)
library(tidycensus)
library(kableExtra)
Data
#Load US Census region data
census_regions <- readxl::read_excel(here::here("data/raw/Census_Data_SVI/census_regions.xlsx"))
# View divisions
census_regions %>% select(Division) %>% distinct()
## # A tibble: 9 × 1
## Division
## <chr>
## 1 New England Division
## 2 Middle Atlantic Division
## 3 East North Central Division
## 4 West North Central Division
## 5 South Atlantic Division
## 6 East South Central Division
## 7 West South Central Division
## 8 Mountain Division
## 9 Pacific Division
import::here( "census_division",
# notice the use of here::here() that points to the .R file
# where all these R objects are created
.from = here::here("analysis/project_data_steps_courtney.R"),
.character_only = TRUE)
census_division
## [1] "Middle Atlantic Division"
# Load API key, assign to TidyCensus Package, remember do not print output
source(here::here("analysis/password.R"))
tidycensus::census_api_key(census_api_key)
Census Variable Data Dictionary
census_variables <- load_variables(2020, "acs5/subject", cache = TRUE)
census_variables %>% head() %>% kbl() %>% kable_styling() %>% scroll_box(width = "100%")
name
|
label
|
concept
|
S0101_C01_001
|
Estimate!!Total!!Total population
|
AGE AND SEX
|
S0101_C01_002
|
Estimate!!Total!!Total population!!AGE!!Under 5 years
|
AGE AND SEX
|
S0101_C01_003
|
Estimate!!Total!!Total population!!AGE!!5 to 9 years
|
AGE AND SEX
|
S0101_C01_004
|
Estimate!!Total!!Total population!!AGE!!10 to 14 years
|
AGE AND SEX
|
S0101_C01_005
|
Estimate!!Total!!Total population!!AGE!!15 to 19 years
|
AGE AND SEX
|
S0101_C01_006
|
Estimate!!Total!!Total population!!AGE!!20 to 24 years
|
AGE AND SEX
|
Division Population
# Query Census API via tidyverse
acs_pull <- get_acs(geography = "division",
variables = c("S0101_C01_001", "S0101_C03_001", "S0101_C05_001"),
year = 2020) %>% filter(NAME == census_division)
# Join data set with census_variable df
left_join(acs_pull, census_variables, join_by("variable" == "name")) %>% mutate("year" = "2020") %>% kbl(format.args = list(big.mark = ",")) %>% kable_styling() %>% scroll_box(width = "100%")
GEOID
|
NAME
|
variable
|
estimate
|
moe
|
label
|
concept
|
year
|
2
|
Middle Atlantic Division
|
S0101_C01_001
|
41,195,152
|
NA
|
Estimate!!Total!!Total population
|
AGE AND SEX
|
2020
|
2
|
Middle Atlantic Division
|
S0101_C03_001
|
20,084,449
|
1,631
|
Estimate!!Male!!Total population
|
AGE AND SEX
|
2020
|
2
|
Middle Atlantic Division
|
S0101_C05_001
|
21,110,703
|
1,632
|
Estimate!!Female!!Total population
|
AGE AND SEX
|
2020
|