Get the negative values for median household income from 15-digit FIPS based on block groups

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 <- "" # 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])

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


Is -666666666 a code for an issue with the data rather than a data value?

Since the median_income data is taken as is, the result is not anything being done in the script. When an api returns a variable, it cannot know what client is consuming it, so it cannot indictare missing data with a string because at least some clients will interpret the result as typeof character. It can't rely on the client having an NA datatype, so it can't use that, either. The usual recourse is to return a number that encodes missingness. The number needs to be obviously extinct from data. For data restricted to the real number line > 0, one way is to make it negative. For data that can be negative, another way is to make the value so large that it would not be confused with actual data.

Why is the data missing. There are two possibilities:

  1. The 180973801034002 doesn't exist or, perhaps, once existed but doesn't in the specific query being made.
  2. and/or it is a census block with zero population enumerated, so no data has been collected.

This can be checked by using an URL like this.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.