Skip to content

Instantly share code, notes, and snippets.

@mfherman
Last active October 26, 2022 21:10
Show Gist options
  • Save mfherman/7352ebe0d2c2ee7de6c5374dc4c8effd to your computer and use it in GitHub Desktop.
Save mfherman/7352ebe0d2c2ee7de6c5374dc4c8effd to your computer and use it in GitHub Desktop.
automatically find year of most recent acs data available via api and use as input parameter to get_acs()
library(tidyverse)
library(tidycensus)
library(jsonlite)
# get all available census api endpoints
census_api_discovery <- fromJSON("https://api.census.gov/data.json")
# parse responses into df with vintage and dataset name
api_name_vintage <- tibble(
vintage = census_api_discovery$dataset$c_vintage,
dataset = map_chr(census_api_discovery$dataset$c_dataset, paste, collapse = "-")
)
# find max year of acs 1
max_year_acs_1 <- api_name_vintage|>
filter(dataset == "acs-acs1-subject") |>
pull(vintage) |>
max()
# find max year of acs 5
max_year_acs_5 <- api_name_vintage|>
filter(dataset == "acs-acs5-subject") |>
pull(vintage) |>
max()
# pull most recent acs 1 data
get_acs(
geography = "state",
variables = "B01003_001",
year = max_year_acs_1,
survey = "acs1"
)
# pull most recent acs 5 data
get_acs(
geography = "state",
variables = "B01003_001",
year = max_year_acs_5,
survey = "acs5"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment