Geo*_*m B 4 loops compiler-errors r function census
我希望使用 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)
})
Run Code Online (Sandbox Code Playgroud)
由此产生的错误是
No encoding supplied: defaulting to UTF-8.
Error: parse error: premature EOF
(right here) ------^
Run Code Online (Sandbox Code Playgroud)
将每个州内的县转换为列表的类似方法也不起作用
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)
})
Run Code Online (Sandbox Code Playgroud)
Error: Result 1 is not a length 1 atomic vector 被退回。
有没有人了解如何完成?我很可能没有正确使用函数或语法,而且我也不太擅长循环。任何帮助,将不胜感激。
解决方案由tidycensus(Kyle Walker)作者提供,如下:
不幸的是,这暂时不起作用。如果它确实有效,您的代码将需要在由评估的函数中识别每个州内的县
map_df,然后将数据集逐县和逐州拼接在一起。问题是区块组数据仅按县提供,因此您需要依次遍历美国所有 3000 多个县。如果它确实有效,成功的调用将如下所示:
library(tigris)
library(tidyverse)
library(tidycensus)
library(sf)
ctys <- counties(cb = TRUE)
state_codes <- unique(fips_codes$state_code)[1:51]
bgs <- map_df(state_codes, function(state_code) {
state <- filter(ctys, STATEFP == state_code)
county_codes <- state$COUNTYFP
get_acs(geography = "block group", variables = "B25038_001",
state = state_code, county = county_codes)
})
Run Code Online (Sandbox Code Playgroud)
问题是,虽然我有内部逻辑来允许多州呼叫或一个州内的多县呼叫,但 tidycensus 还不能同时处理多州和多县呼叫。
尝试这个包:totalcensushttps : //github.com/GL-Li/totalcensus。它将人口普查数据文件下载到您自己的计算机,并从这些文件中提取任何数据。设置文件夹和路径后,如果您想要 2015 ACS 5 年调查中的所有块组数据,请运行以下代码。
library(totalcensus)
# download the 2015 ACS 5-year survey data, which is about 50 GB.
download_census("acs5year", 2015)
# read block group data of variable B25038_001 from all states plus DC
block_groups <- read_acs5year(
year = 2015,
states = states_DC,
table_contents = "B25038_001",
summary_level = "block group"
)
Run Code Online (Sandbox Code Playgroud)
提取所有状态和DC的217739个块组的数据:
# GEOID lon lat state population B25038_001 GEOCOMP SUMLEV NAME
# 1: 15000US020130001001 -164.1232 54.80448 AK 982 91 all 150 Block Group 1, Census Tract 1, Aleutians East Borough, Alaska
# 2: 15000US020130001002 -161.1786 55.60224 AK 1116 247 all 150 Block Group 2, Census Tract 1, Aleutians East Borough, Alaska
# 3: 15000US020130001003 -160.0655 55.13399 AK 1206 352 all 150 Block Group 3, Census Tract 1, Aleutians East Borough, Alaska
# 4: 15000US020160001001 178.3388 51.95945 AK 1065 264 all 150 Block Group 1, Census Tract 1, Aleutians West Census Area, Alaska
# 5: 15000US020160002001 -166.8899 53.85881 AK 2038 380 all 150 Block Group 1, Census Tract 2, Aleutians West Census Area, Alaska
# ---
# 217735: 15000US560459511001 -104.7889 43.99520 WY 1392 651 all 150 Block Group 1, Census Tract 9511, Weston County, Wyoming
# 217736: 15000US560459511002 -104.4785 43.76853 WY 2050 742 all 150 Block Group 2, Census Tract 9511, Weston County, Wyoming
# 217737: 15000US560459513001 -104.2575 43.88160 WY 1291 520 all 150 Block Group 1, Census Tract 9513, Weston County, Wyoming
# 217738: 15000US560459513002 -104.1807 43.85406 WY 1046 526 all 150 Block Group 2, Census Tract 9513, Weston County, Wyoming
# 217739: 15000US560459513003 -104.2601 43.84355 WY 1373 547 all 150 Block Group 3, Census Tract 9513, Weston County, Wyoming
Run Code Online (Sandbox Code Playgroud)