r tidycensus download all block groups

Viewed 1937

I am looking to automate the process of downloading Census data from all block groups from the US using the tidycensus package. There is instructions from the developer to download all tracts within the US, however, block groups cannot be accessed using the same method.

Here is my current code that does not work

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

# create lists of state and county codes

data("fips_codes")
temp <- data.frame(state = as.character(fips_codes$state_code),
                   county = fips_codes$county_code,
                   stringsAsFactors = F)
temp <- aggregate(county~state, temp, c)
state <- temp$state
coun <- temp$county

# use map2_df to loop through the files, similar to the "tract" data pull

home <- map2_df(state, coun, function(x,y) {
get_acs(geography = "block group", variables = "B25038_001", #random var
state = x,county = y)
  })

The resulting error is

No encoding supplied: defaulting to UTF-8.
Error: parse error: premature EOF

                     (right here) ------^

A similar approach to convert the counties within each state into a list also does not work

temp <- aggregate(county~state, temp, c)
state <- temp$state
coun <- temp$county

df<- map2_df(state, coun, function(x,y) {
    get_acs(geography = "block group", variables = "B25038_001", 
            state = x,county = y)
  })

Error: Result 1 is not a length 1 atomic vector is returned.

Does anyone have an understanding of how this could be completed? More than likely I am not using functions properly or syntax, and I am also not very good with loops. Any help would be appreciated.

2 Answers
Related