4

I am trying to load variable data in with tidycensus but am running into a pervasive error.

library(tidycensus)
library(tidyverse)
census_api_key("My key is here")

poverty <- get_acs(geography = "block group", variables = "B17021",
                state = "MO", county = "St. Louis City", geometry = TRUE)

The error I get is this.

Error in as.character(x) : cannot coerce type 'closure' to vector of type 
'character'

I have made sure I am running up to date packages and have input my Census API Key, but the error persists. Any suggestions appreciated

2 Answers2

2

You cannot access variables through the Census Bureau API without specifying what "row" and "column" you want. From table B17021 you have many options; one such option you could select would be the Estimate (column) of Income in the past 12 months below poverty level (row). In general, the format of properly formatted variable names is TableName_VariableNumberType. So for the example I just gave:

  • Table = B17021
  • Row = 002
  • Type = estimate (E)

The variable this corresponds to is "B17021_002E".

However, it's best to simply consult the relevant API documentation to make sure you have the correct variable name. Table B17021 begins about halfway down the page, and you can cross-reference this guide against what you see the in the American FactFinder to make sure you have the right variable name.

As an aside, I wrote myself a guide about this a couple of years ago for Python. It is slightly out of date, but I have posted it to my GitHub in case it might help.

Edit: It appears that get_acs does not require you put in the variable type. If you do not, the data that are returned have all attributes, which allows you to separately plot estimate, margin of error, from the same object. That's a nice feature!

HFBrowning
  • 2,196
  • 3
  • 23
  • 42
1

The answer by @HFBrowning is great. Also: you can use the table parameter instead of the variables parameter to request all variables from a given table, which will allow your supplied code to work. For example:

poverty <- get_acs(geography = "block group", table = "B17021",
                   state = "MO", county = "St. Louis City", geometry = TRUE)

works, and allows you to get variables B17021_001 through B17021_035 without specifying them individually.

kwalkertcu
  • 1,011
  • 6
  • 8