Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mali543 opened this issue Apr 19, 2019 · 2 comments

Comments

@mali543
Copy link

mali543 commented Apr 19, 2019

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

@mali543 mali543 changed the title Bug report: Bug report: error getting ACS county-to-county migration flow data for all states Apr 19, 2019
@hrecht
Copy link
Owner

hrecht commented Apr 20, 2019

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
Copy link
Author

mali543 commented Apr 20, 2019

That worked very well! Thank you so much Hannah.

@hrecht hrecht closed this as completed Apr 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants