r tidycensus下载所有块组 [英] r tidycensus download all block groups

查看:517
本文介绍了r tidycensus下载所有块组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用tidycensus包自动执行从美国所有区块组下载人口普查数据的过程。开发人员可以下载美国版中的所有作品集但是,无法使用相同的方法访问块组。



这是我目前无法使用的代码

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

#创建州和县代码列表

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

#使用map2_df遍历文件,类似到道数据pull

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

产生的错误是:

 没有提供编码:默认为UTF-8。 
错误:解析错误:过早EOF

(在这里)------ ^

将每个州内的县转换成列表的类似方法也无效

  temp < - 聚合(县〜州,临时,c)
州 - 临时$州
国家 - 临时$县

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

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



有没有人了解如何完成这项工作?更可能的是我没有正确使用函数或语法,而且我也不太擅长循环。任何帮助将不胜感激。

https://github.com/GL-Li/totalcensus 。它将人口普查数据文件下载到您自己的计算机,并从这些文件中提取任何数据。设置文件夹和路径后,运行下面的代码,如果您想在2015 ACS 5年调查中获得所有的块组数据。

 图书馆(totalcensus)

#下载2015年ACS 5年调查数据,大约50 GB。
download_census(acs5year,2015)

#从所有状态读取变量B25038_001的块组数据加上DC
block_groups< - read_acs5year(
year = 2015 ,
states = states_DC,
table_contents =B25038_001,
summary_level =block group

提取所有州和DC 217739块组的数据:

  #GEOID lon lat state population B25038_001 GEOCOMP SUMLEV名称
#1:15000US020130001001 -164.1232 54.80448 AK 982 91全部150 Block Group 1,人口普查区1,Aleutians East Borough,Alaska
#2:15000US020130001002 -161.1786 55.60224 AK 1116 247全部150 Block Group 2,人口普查区1,Aleutians East Borough,阿拉斯加
#3:15000US020130001003 -160.0655 55.13399 AK 1206 352全部150 Block Group 3,人口普查区1,Aleutians东部自治区,阿拉斯加
#4:15000US020160001001 178.3388 51.95945 AK 1065 264全部150区块1,人口普查区1,Aleutians西部人口普查区,阿拉斯加
# 5:15000US020160002001 -166.8899 53.85881 AK 2038 380全部150座第1组,人口普查区2,Aleutians West人口普查区,阿拉斯加
#---
#217735:15000US560459511001 -104.7889 43.99520 WY 1392 651全部150座怀俄明州Weston县Census Tract 9511组1,人口普查区9511
#217736:15000US560459511002 -104.4785 43.76853 WY 2050 742全部150 Block 2,人口普查区9511,怀俄明州韦斯顿县
#217737:15000US560459513001 -104.2575 43.88160 WY 1291 52 0全部150 Block Group 1,Census Tract 9513,Weston County,Wyoming
#217738:15000US560459513002 -104.1807 43.85406 WY 1046 526全部150 Block 2 Group,2人口普查区9513,Weston County,Wyoming
#217739: 15000US560459513003 -104.2601 43.84355 WY 1373 547全部150 Block 3,人口普查区9513,怀俄明州韦斯顿县


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.

解决方案

Try this package: totalcensus at https://github.com/GL-Li/totalcensus. It downloads census data files to your own computer and extracts any data from these files. After set up folders and path, run the code below if you want all block group data in 2015 ACS 5-year survey.

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

The extracted data of 217739 block groups of all states and DC:

    #                       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

这篇关于r tidycensus下载所有块组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆