hrecht / censusapi

R package to retrieve U.S. Census data and metadata via API
https://www.hrecht.com/censusapi/
169 stars 30 forks source link

Rapidly Gathering Block Data #71

Closed braedenmc1 closed 3 years ago

braedenmc1 commented 3 years ago

Describe the bug I am trying to manipulate the example of gathering block data in the README, but apply it to collecting data from the 2000 Decennial Census as a whole. I'm not sure I'm on the right track, but I will post the code below

To Reproduce

data2000 <- getCensus(
  key = Sys.getenv("census_key"),
  name = "dec/sf1",
  vintage = 2000,
  vars = "P001001", 
  region = "block:*",
  regionin = "state: + county: + tract:")

Expected behavior I was hoping that linking the region measures together would pull data for the entire US, but I get a long error code.

R session information:

Additional context My goal is to gather information on the Latino population at the block level, but I wanted to reproduce the example to collect data from all the blocks before adding the variable I want to explore.

hrecht commented 3 years ago

Deleted a previous reply - Census actually seems to have made this easier recently! regionin = "state: + county: + tract:" is not valid code, you need to use fips codes and/or wildcards. You need to specify state here but can use wildcards for county and tract (to get every block in a single state in one call.)

You can do this in a for loop like the example here. https://hrecht.github.io/censusapi/articles/getting-started.html#advanced-geographies

blocks <- NULL
for (f in fips) {
    stateget <- paste("state:", f, "+county:*+tract:*", sep="")
    print(f)
    temp <- getCensus(
        name = "dec/sf1",
        vintage = 2000,
        vars = "P001001",
        region = "block:*",
        regionin = stateget)
    blocks <- rbind(blocks, temp)
}

Note: this will take a while to run. Unless you really really need block-level data I'd recommend block group or tract. Not much detailed data is available at the block level. You could also download bulk data from NHGIS.

braedenmc1 commented 3 years ago

Thank you for the help! I just realized I need block-group data. My apologies for sending you in the wrong direction. Would the best approach still be using the function you posted in your response for the block group data? If not, is there another approach you would recommend? This is my first time using data at this level, so I am still getting the terminology straight.

Another quick follow up question:

I am also looking for the same variable for state-level House districts (not Congressional districts. Would this data be found in the "dec/sldhprofile" API or does the sec/sf1 API contain information on this level of analysis as well?

braedenmc1 commented 3 years ago

Hello!

I have the specific code I want to return from the 2000 Decennial state legislative file.

#Get Latino District Population
leg_test <- getCensus(
  key = Sys.getenv("census_key"),
  name = "dec/sldhprofile",
  vintage = 2000, 
  vars = "DP1_C100",
  region = "state", 
  regionin = "state legislative district (lower chamber)")

I read the variables from this API after manipulating the listCensusMetadata argument for this particular API. The lower state house district required the state argument to run, but after adding the region and region in arguments the way the variables appeared in the API, I still received an error.

hrecht commented 3 years ago

You need to specify the region and regionin arguments, in your case with the wildcard. So regionin = "state:*" etc. Here's an intro article that should help: https://hrecht.github.io/censusapi/articles/getting-started.html You also have region and regionin backwards, legislative district is within state, not the other way around.