我希望使用 tidycensus 包从美国的所有区块组下载人口普查数据的过程自动化。开发人员提供了下载美国境内所有区域的说明,但是无法使用相同的方法访问块组。
这是我当前不起作用的代码
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)
})
产生的错误是
No encoding supplied: defaulting to UTF-8.
Error: parse error: premature EOF
(right here) ------^
将每个州内的县转换为列表的类似方法也不起作用
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
被退回。
有没有人了解如何完成?很可能我没有正确使用函数或语法,而且我也不太擅长循环。任何帮助,将不胜感激。