hrecht / censusapi

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

Bug report: error getting ACS county-to-county migration flow data for all states #53

Closed mali543 closed 5 years ago

mali543 commented 5 years ago

I am trying to get ACS county-to-county migration flow data using censusapi following your example here

I have successfully downloaded ACS migration flow data for 2016 using the following code:

vars <-c("MOVEDIN", "MOVEDOUT", "FULL1_NAME", "FULL2_NAME", "GEOID2") migr16 <- getCensus(name = "acs/flows", vintage = 2016, vars = vars, region = "county“) However, this does not work for other years than 2016. For 2015 it only works if I add "regionin" for one state:

migr15 <- getCensus(name = "acs/flows", vintage = 2015, vars = vars, region = "county", regionin = "state:01“) As I want to download data for all counties I tried looping it using map_dfr as suggested here:

mystates <- c(paste0("state:", str_pad(1:51, 2, pad="0"))) migr15 <- map_dfr(mystates, ~ getCensus(name = "acs/flows", vintage = 2015, vars = vars, region = "county", regionin = .x) ) I also tried other options as suggested in the post. None of them worked when using censusapi. I would highly appreciate your help!

Censusapi package version: 0.6.0 R version: 3.5.1

hrecht commented 5 years ago

The 2015 county data is not available for all states at once - the county geography is nested within state for that year, which can be seen with listCensusMetadata(name = "acs/flows", vintage = 2015, type = "geographies") (or https://api.census.gov/data/2015/acs/flows/geography.html)

I'm not super familiar with map_dfr, but another method is using a for loop over the built-in list of state fips codes, like this example: https://hrecht.github.io/censusapi/articles/getting-started.html#advanced-geographies

library(censusapi)
counties <- NULL
for (f in fips) {
    stateget <- paste("state:", f, sep="")
    temp <- getCensus(name = "acs/flows",
      vintage = 2015,
      vars = c("MOVEDIN", "MOVEDOUT", "FULL1_NAME", "FULL2_NAME", "GEOID2"),
      region = "county:*",
      regionin = stateget)
    counties <- rbind(counties, temp)
}
head(counties)

This takes a few minutes because it's a lot of data, but it worked for me.

mali543 commented 5 years ago

That worked very well! Thank you so much Hannah.