if (FALSE) {
# Requires Census Bureau API key
# Get the median household income by tract for Washington DC and join
# this data with DC tract boundaries.
library(data.table)
library(httr2)
library(jsonlite)
library(sf)
library(usmap)
library(withr)
library(ggplot2)
library(RcensusPkg)
# Get the 2020 median household income data by tract for DC
dc_fips <- usmap::fips(state = "dc")
dc_B19013_dt <- RcensusPkg::get_vintage_data(
dataset = "acs/acs5",
vintage = 2020,
vars = "B19013_001E",
region = "tract",
regionin = paste0("state:", dc_fips)
)
# Get the simple feature DC tract geometries and join the data dataframe "dc_B19013_dt"
output_dir <- withr::local_tempdir()
if(!dir.exists(output_dir)){
dir.create(output_dir)
}
dc_tracts_sf <- RcensusPkg::tiger_tracts_sf(
state = dc_fips,
output_dir = output_dir,
general = TRUE,
delete_files = FALSE
)
# Join the data with simple feature object
dc_joined_sf <- RcensusPkg::join_it(
df_1 = dc_B19013_dt,
df_2 = dc_tracts_sf,
key_1 = "GEOID",
key_2 = "GEOID",
return_sf = TRUE
)
}
Run the code above in your browser using DataLab