Aggregate block-group ACS indicators up to tract, county, and state
Source:R/calc_acs_by_geography.R
calc_acs_by_geography.RdPart (c) of issue #395: roll block-group ACS demographic values up to tract,
county, and state for the EJSCREEN web app's "additional demographics" (and
side-by-side) layers, using the same per-indicator aggregation rules as
doaggregate().
Usage
calc_acs_by_geography(
bg = blockgroupstats,
id_col = "bgfips",
levels = c("blockgroup", "tract", "county", "state"),
pop_fallback = TRUE,
exclude_islandareas = TRUE,
out_dir = NULL
)Arguments
- bg
Block-group table with
id_coland ACS indicator columns. Defaults to blockgroupstats. Not modified (a copy is made internally).- id_col
Block-group FIPS column name. Default
"bgfips"; also used as the id column name in the block-group-level output. If the column is numeric, its leading zero is restored before parent GEOIDs are derived from it.- levels
Any of
"blockgroup","tract","county","state".- pop_fallback
If
TRUE(default), weighted-mean indicators whose weight column is missing from the data fall back to population weighting, as indoaggregate(). IfFALSE, such indicators are dropped.- exclude_islandareas
If
TRUE(default), drop Island Area block groups (AS/GU/MP/VI/UM) before aggregating – identified by theSTcolumn viais.island()when present (their FIPS are non-standard length). SetFALSEto keep them (they will have0/NAdemographics).- out_dir
Optional directory; if set, each level is written there as
acs_by_<level>.csv.
Value
A named list of data.frames, one per requested level: the geography FIPS id column, the summed count columns, and the recomputed weighted-mean indicator columns.
Details
Each indicator is aggregated by the method EJAM stores for it, looked up with
the same accessors doaggregate() uses:
calctype()== "sum of counts"-> the column is summed within each geography (population, households, the ACS universes/denominators, raw counts, etc.).calctype()== "wtdmean"-> the column is a weighted mean, where the weight is the indicator's own denominator fromcalcweight()(for examplepopfor population fractions,hhlds/occupiedunits/builtunitsfor household-based indicators,povknownratiofor the low-income fraction,age25upfor educational attainment, and so on). The mean issum(value * weight) / sum(weight), taken only over block groups that have the indicator.
Following doaggregate(), if an indicator's weight column is not present in
the data, the population weight pop is used as a fallback (with a message).
Percentile (lookedup), ratio to avg, and map bin/text columns are not
recomputed here – those need geography-level distributions / lookups and are a
separate step (see issue #395). The geography id is the leading FIPS digits of
bgfips: 12 = block group, 11 = tract, 5 = county, 2 = state.
Island Areas (American Samoa, Guam, the Northern Mariana Islands, the U.S.
Virgin Islands) have no standard ACS demographics, so by default
(exclude_islandareas = TRUE) their block groups are dropped before
aggregating. This matches EJAM's v3 decision that Island Area demographics are
unavailable by default (rather than emitting them with na.rm sums of 0), and
matches EPA's ACS layer, which omits them. Puerto Rico is not an Island Area for
this purpose (it has standard ACS demographics) and is retained.