Skip to contents

download ACS 5year data from Census API, at block group resolution (slowly for entire US)

Usage

acs_bybg(
  variables = c(pop = "B01001_001"),
  table = NULL,
  cache_table = FALSE,
  year = 2022,
  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

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

Value

data.table (not tibble, and not just a data.frame)

Examples

## 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

newvars <- acs_bybg(variables = c("B01001_001", paste0("B01001_0", 31:39)),
  state = "DC")
setnames(newvars, "GEOID", "bgfips")
newvars <- newvars[blockgroupstats[, .(bgfips, ST)], , on = "bgfips"]
names(newvars) <- gsub("E$", "", names(newvars))
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"))
newvars[ST == "DC", ]