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
- 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
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")
#> Error in acs_bybg(c(pop = "B01001_001", y = "B01001_002"), state = "DC"): this requires having set up a census api key - see ?tidycensus::census_api_key
## Format new data to match rows of blockgroupstats
setnames(newvars, "GEOID", "bgfips")
#> Error in setnames(newvars, "GEOID", "bgfips"): could not find function "setnames"
dim(newvars)
#> Error: object 'newvars' not found
newvars <- newvars[blockgroupstats[,.(bgfips, ST)], , on = "bgfips"]
#> Error: object 'newvars' not found
dim(blockgroupstats)
#> [1] 242336 150
dim(newvars)
#> Error: object 'newvars' not found
newvars
#> Error: object 'newvars' not found
newvars[ST == "DC", ]
#> Error: object 'newvars' not found
## 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)
#> Error in acs_bybg(variables = c("B01001_001", paste0("B01001_0", 31:39)), state = mystates): this requires having set up a census api key - see ?tidycensus::census_api_key
setnames(newvars, "GEOID", "bgfips")
#> Error in setnames(newvars, "GEOID", "bgfips"): could not find function "setnames"
newvars[, ST := fips2state_abbrev(bgfips)]
#> Error: object 'newvars' not found
names(newvars) <- gsub("E$", "", names(newvars))
#> Error: object 'newvars' not found
# 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'))
#> Error: object 'newvars' not found
newvars[, pct1849female := round(100 * pct1849female, 1)]
#> Error: object 'newvars' not found
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'))))
#> Error: object 'newvars' not found
# }