download ACS 5year data from Census API, at block group resolution (slowly if for entire US)
Source:R/acs_bybg.R
acs_bybg.Rd
download ACS 5year data from Census API, at block group resolution (slowly if for entire US)
Usage
acs_bybg(
variables = c(pop = "B01001_001"),
table = NULL,
cache_table = FALSE,
year = NULL,
output = "wide",
state = stateinfo$ST,
county = NULL,
zcta = NULL,
geometry = FALSE,
keep_geo_vars = FALSE,
summary_var = NULL,
key = NULL,
moe_level = 90,
survey = "acs5",
show_call = FALSE,
geography = "block group",
dropname = TRUE,
...
)
Arguments
- variables
Vector of variables - see get_acs from tidycensus package
- table
see get_acs from tidycensus package.
EJSCREEN-relevant key tables at block group resolution include these: acstabs <- c("B01001", "B03002", "B15002", "C16002", "C17002", "B25034", "B23025") and at tract resolution: "B18101"
- cache_table
see get_acs from tidycensus package
- year
e.g., 2022 see get_acs from tidycensus package, and the helper function in the EJAM package called
acsendyear()
- output
see get_acs from tidycensus package
- state
Default is 2-character abbreviations, vector of all US States, DC, and PR.
- county
see get_acs from tidycensus package
- zcta
see get_acs from tidycensus package
- geometry
see get_acs from tidycensus package
- keep_geo_vars
see get_acs from tidycensus package
- summary_var
see get_acs from tidycensus package
- key
see get_acs from tidycensus package
- moe_level
see get_acs from tidycensus package
- survey
see get_acs from tidycensus package
- show_call
see get_acs from tidycensus package
- geography
"block group"
- dropname
whether to drop the column called NAME
- ...
see get_acs from tidycensus package
Details
Probably requires getting and specifying an API key for Census Bureau ! (at least if query is large). see tidycensus package help
NOTES ON KEY TABLES IN ACS THAT ARE RELEVANT TO EJSCREEN:
x <- tidycensus::load_variables(2022, "acs5")
acstabs <- c("B01001", "B03002", "B15002", "C16002", "C17002", "B25034", "B23025",
"B18101") # disability at tract resolution only
acstabs2 <- paste0(acstabs, "_")
mytables <- data.table::rbindlist(lapply(acstabs2, function(z) {x[substr(x$name,1,7) %in% z, ][1,]}))
print(mytables)
name label concept geography
<char> <char> <char> <char>
1: B01001_001 Estimate!!Total: Sex by Age block group
2: B03002_001 Estimate!!Total: Hispanic or Latino Origin by Race block group
3: B15002_001 Estimate!!Total: Sex by Educational Attainment for the Population 25 Years and Over block group
4: C16002_001 Estimate!!Total: Household Language by Household Limited English Speaking Status block group
5: C17002_001 Estimate!!Total: Ratio of Income to Poverty Level in the Past 12 Months block group
6: B25034_001 Estimate!!Total: Year Structure Built block group
7: B23025_001 Estimate!!Total: Employment Status for the Population 16 Years and Over block group
8: B18101_001 Estimate!!Total: Sex by Age by Disability Status tract
# see details of the variables
x[substr(x$name,1,7) %in% "B01001_", ] |> print(n=50)
x[substr(x$name,1,7) %in% "B03002_", ] |> print(n=50)
x[substr(x$name,1,7) %in% "B15002_", ] |> print(n=50)
x[substr(x$name,1,7) %in% "C16002_", ] |> print(n=50)
x[substr(x$name,1,7) %in% "C17002_", ] |> print(n=50)
x[substr(x$name,1,7) %in% "B25034_", ] |> print(n=50)
x[substr(x$name,1,7) %in% "B23025_", ] |> print(n=50)
x[substr(x$name,1,7) %in% "B18101_", ] |> print(n=50)
# disability is by tract only:
cbind(unique(grep("disab", x$concept, value = T, ignore.case = T) ))
# x[substr(x$name,1,6) %in% "B18101" & x$geography == "block group", ] |> print(n=50) # none
x[substr(x$name,1,7) %in% "B18101_" , ] |> print(n=50)
Examples
# \donttest{
## All states, full table
# newvars <- acs_bybg(table = "B01001")
## One state, some variables
newvars <- acs_bybg(c(pop = "B01001_001", y = "B01001_002"), state = "DC")
## Format new data to match rows of blockgroupstats
setnames(newvars, "GEOID", "bgfips")
dim(newvars)
newvars <- newvars[blockgroupstats[,.(bgfips, ST)], , on = "bgfips"]
dim(blockgroupstats)
dim(newvars)
newvars
newvars[ST == "DC", ]
## Calculate a new indicator for each block group, using ACS data
mystates = c("DC", 'RI')
newvars <- acs_bybg(variables = c("B01001_001", paste0("B01001_0", 31:39)),
state = mystates)
setnames(newvars, "GEOID", "bgfips")
newvars[, ST := fips2state_abbrev(bgfips)]
names(newvars) <- gsub("E$", "", names(newvars))
# provide formulas for calculating new indicators from ACS raw data:
formula1 <- c(
" pop = B01001_001",
" age1849female = (B01001_031 + B01001_032 + B01001_033 + B01001_034 +
B01001_035 + B01001_036 + B01001_037 + B01001_038 + B01001_039)",
" pct1849female = ifelse(pop == 0, 0, age1849female / pop)"
)
newvars <- calc_ejam(newvars, formulas = formula1,
keep.old = c("bgid", "ST", "pop", 'bgfips'))
newvars[, pct1849female := round(100 * pct1849female, 1)]
mapfast(newvars[1:10,], column_names = colnames(newvars),
labels = gsub('pct1849female', 'Women 18-49 as % of residents',
gsub('age1849female', 'Count of women ages 18-49',
fixcolnames(colnames(newvars), 'r', 'long'))))
# }