gpt4 book ai didi

r tidycensus 下载所有 block 组

转载 作者:行者123 更新时间:2023-12-02 09:44:08 24 4
gpt4 key购买 nike

我希望使用 tidycensus 软件包自动执行从美国所有区 block 组下载人口普查数据的过程。开发人员指示下载US内的所有单张。但是,无法使用相同的method来访问 block 组。 。

这是我当前不起作用的代码

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)
})

错误:返回结果 1 不是长度为 1 的原子向量

有人知道如何完成这个任务吗?很可能我没有正确使用函数或语法,而且我也不太擅长循环。任何帮助将不胜感激。

最佳答案

解决方案由tidycensus的作者(Kyle Walker)提供,如下:

Unfortunately this just doesn't work at the moment. If it did work, your code would need to identify the counties within each state within a function evaluated by map_df and then stitch together the dataset county-by-county, and state-by-state. The issue is that block group data is only available by county, so you'd need to walk through all 3000+ counties in the US in turn. If it did work, a successful call would look like this:

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)
})

The issue is that while I have internal logic to allow for multi-state calls, or multi-county calls within a state, tidycensus can't yet handle multi-state and multi-county calls simultaneously.

关于r tidycensus 下载所有 block 组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45109241/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com