0

关于如何最好地使用 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 = "pretty", 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(estimate(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)

4

2 回答 2

0

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

> 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。知道为什么会这样吗?

于 2021-08-13T03:36:03.223 回答
0

您可以在以下链接下载 2010 表 shell 中所有代码的副本。当您单击它时,它将开始下载和 Excel 文件。

ACS 表壳 下载

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

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

于 2016-05-04T19:06:20.347 回答