Hello all, I'm trying to extract some demographic data with 15-digit FIPS at the block group level. However, I got some negative values from it? Can you help me with it? Appreciate it.
# Define your API key
api_key <- "94d96c1dfb04afe285d0db958f78ded8a4b197f7"
# Function to extract median household income from Census API for a given block group geoid
get_median_income <- function(fips) {
# Extract state, county, and block group from the FIPS code
state <- substr(fips, 1, 2)
county <- substr(fips, 3, 5)
tract <- substr(fips, 6, 11)
block_group <- substr(fips, 12, 12)
# Define the URL for the API request
base_url <- "https://api.census.gov/data/2021/acs/acs5" # updated year to 2021
variables <- "B19013_001E" # Median household income variable
url <- paste0(base_url, "?get=", variables, "&for=block%20group:", block_group, "&in=state:", state, "+county:", county, "+tract:", tract, "&key=", api_key)
# Make the API request
response <- GET(url)
data <- fromJSON(content(response, as="text"))
# Extract the median income from the data
median_income <- as.numeric(data[[2]][1])
return(median_income)
}
# Sample data
sample_fips <- tibble(block_fips = c("060730073042001", "720330204282000",
"180973801034002", "110010003001005", "481576729033029", "484391136123003"))
# Extract median incomes for each block FIPS
test <- sample_fips %>%
rowwise() %>%
mutate(median_income = get_median_income(block_fips)) %>%
ungroup()