Problems Fetching 5-Year Zip-Level Data w API

Hi All,

I've been successfully fetching ACS 5-year state legislative district data using my API key for years 2010 - 2022.

However, I am having issues fetching the same data at the zip-level (for years 2011 - 2022). I am able to fetch zip-level data for years 2011 - 2018, but when I try to add 2019 - 2022, I receive an error that says my API key is invalid. 

Below is the code that doesn't work for 2011 - 2022.

Thanks for any help!

# Install required packages
if (!require(tidycensus)) install.packages("tidycensus")
if (!require(dplyr)) install.packages("dplyr")

# Load the packages
library(tidycensus)
library(dplyr)

# Set your Census API key
census_api_key("your_api_key", install = TRUE, overwrite = TRUE)

# Define the years you're interested in (2011 to 2022)
years <- 2011:2022
states <- c("CO", "AZ", "ID", "MT", "NV", "NM", "UT", "WY")

# Function to fetch population data for a specific year and state
get_population_data <- function(year, state) {
tryCatch({
data <- get_acs(geography = "zip code tabulation area",
state = state,
year = year,
survey = "acs5",
table = "B01003",
output = "wide") %>%
mutate(Year = year, State = state)
return(data)
}, error = function(e) {
message("Error with year ", year, ", state ", state, ": ", e$message)
return(NULL)
})
}

# Initialize an empty list to store the data
all_data <- list()

# Loop over each year and state, fetching data
for (state in states) {
for (year in years) {
data <- get_population_data(year, state)
if (!is.null(data)) {
all_data[[paste(state, year)]] <- data
}
}
}

# Combine the data from different years and states
combined_data <- bind_rows(all_data)

Parents
  • As said they are no longer nested in states. They stopped being nested in states in 2020. 

    It may not help you since it's in Python, but the censusdis package can do all of this for you, or will be able to as of the next release. Take a look at the notebook at 

    https://github.com/vengroff/censusdis/blob/main/notebooks/Zip%20Code%20Tabulation%20Areas.ipynb.

    I even changed it to use the states in your example.

    Pre-2020, when they were nested in states, it's

    ced.download(
        ACS5,
        year,
        ["NAME"],
        group="B01003",
        state=states,
        zip_code_tabulation_area="*",
        with_geometry=True,
    )

    and for 2020 and later, it's the slightly more complicated

    ced.contained_within(state=states).download(
        ACS5,
        year,
        ["NAME"],
        group="B01003",
        zip_code_tabulation_area="*",
        with_geometry=True,
    )

    The 2022 results look like this on a map:



    For your other question, for large groups where you don't want all the variables, you can just list the individual variables you want in the list with `"NAME"` and take out the `group=` arg. Or, for a group like B03002 that has an internal tree structure, you can use `leaves_of_group=` arg instead of `group=` and it will get you just the leaves of the tree, not the internal aggregates. 

Reply
  • As said they are no longer nested in states. They stopped being nested in states in 2020. 

    It may not help you since it's in Python, but the censusdis package can do all of this for you, or will be able to as of the next release. Take a look at the notebook at 

    https://github.com/vengroff/censusdis/blob/main/notebooks/Zip%20Code%20Tabulation%20Areas.ipynb.

    I even changed it to use the states in your example.

    Pre-2020, when they were nested in states, it's

    ced.download(
        ACS5,
        year,
        ["NAME"],
        group="B01003",
        state=states,
        zip_code_tabulation_area="*",
        with_geometry=True,
    )

    and for 2020 and later, it's the slightly more complicated

    ced.contained_within(state=states).download(
        ACS5,
        year,
        ["NAME"],
        group="B01003",
        zip_code_tabulation_area="*",
        with_geometry=True,
    )

    The 2022 results look like this on a map:



    For your other question, for large groups where you don't want all the variables, you can just list the individual variables you want in the list with `"NAME"` and take out the `group=` arg. Or, for a group like B03002 that has an internal tree structure, you can use `leaves_of_group=` arg instead of `group=` and it will get you just the leaves of the tree, not the internal aggregates. 

Children
No Data