【问题标题】:Extract census data using ACS R package for all Zip codes and obtain future projections使用 ACS R 包为所有邮政编码提取人口普查数据并获得未来预测
【发布时间】:2016-05-03 01:11:54
【问题描述】:

我有几个关于如何最好地使用 acs R 包的问题。在此先感谢您的帮助。

  • 我想建立一个综合数据框,它是一个查找表,其中包含我可以从他们的 API 获得的每个邮政编码的所有人口普查数据。目前我只是使用 R 代码查找几个单独的表,如下例所示。是否有更好的方法来查找所有可用表并自动构建数据表数据集并填充列名?我知道 acs.lookup 函数,但我想加载所有表并获取其邮政编码的数据。有没有办法从 acs.lookup 输出中获取所有表的列表,或者可能是可用表的完整列表?

  • 我还想获得尽可能多的变量的未来预测数据。我想我可以使用上述方法使用多年(2014 年、2013 年、2012 年、2011 年)并使用 2014 年的 acs14lite R 包来计算我发现的预测。在我这样做之前,我想知道美国人口普查本身是否有未来的预测使用这个 ACS 数据还是别的什么?

    创建用户指定的地理位置

    使用所有邮政编码

    zip_geo = geo.make(zip.code = "*")

    创建比赛数据框

    获取比赛数据

    race.data = acs.fetch(geography=zip_geo, table.number = "B03002", col.names = "漂亮", endyear = 2013, span = 5)

    创建人口统计数据框

    zip_demographics = data.frame(region = as.character(geography(race.data)$zipcodetabulationarea), total_population = as.numeric(estimate(race.data[,1])))

    zip_demographics$region = as.character(zip_demographics$region)

    转换为data.frame

    race_df = data.frame(white_alone_not_hispanic = as.numeric(estimate(race.data[,3])), black_alone_not_hispanic = as.numeric(估计(race.data[,4])), asian_alone_not_hispanic = as.numeric(estimate(race.data[,6])), hispanic_all_races = as.numeric(estimate(race.data[,12])))

    zip_demographics$percent_white = (race_df$white_alone_not_hispanic / zip_demographics$total_population * 100) zip_demographics$percent_black = (race_df$black_alone_not_hispanic / zip_demographics$total_population * 100) zip_demographics$percent_asian = (race_df$asian_alone_not_hispanic / zip_demographics$total_population * 100) zip_demographics$percent_hispanic = (race_df$hispanic_all_races / zip_demographics$total_population * 100)

【问题讨论】:

  • 另外,只是一个友好的建议。如果您在帖子中使用按钮来正确格式化代码、链接和图像,人们会发现更容易关注您的帖子并更频繁地回复。

标签: r census


【解决方案1】:

您可以在以下链接下载 2010 表 shell 中所有代码的副本。点击后会开始下载excel文件。

ACS Table Shells Download

我所做的是将此文档加载为数据框,使用适当的代码格式化列,然后使用单元格地址示例:povertyNumerator<acsTable[781,2] 来拉入变量。

您无法完全自动化该过程,因为您需要决定如何分解响应的“类别”并进行自己的数学运算,但除此之外,您可以使用此表和一些 acs 包技能快速工作。

【讨论】:

    【解决方案2】:

    当我尝试运行上面的代码时,我收到以下错误消息:

    > race.data = acs.fetch(geography=zip_geo, table.number = "B03002", col.names = "pretty", endyear = 2013, span = 5)
    trying URL 'http://web.mit.edu/eglenn/www/acs/acs-variables/acs_5yr_2013_var.xml.gz'
    Content type 'application/xml' length 720299 bytes (703 KB)
    downloaded 703 KB
    
    Error in .subset2(x, i, exact = exact) : subscript out of bounds
    In addition: Warning message:
    In (function (endyear, span = 5, dataset = "acs", keyword, table.name,  :
      temporarily downloading and using archived XML variable lookup files;
      since this is *much* slower, recommend running
      acs.tables.install()
    

    并且没有产生输出race.data。知道为什么会这样吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-26
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多