1

我正在尝试使用 R 中的 acs 包下载基本地图的人口普查数据,但我无法下载数据并且收到一条令人困惑的错误消息。

我的代码如下:

#Including all packages here in case this is somehow the issue

install.packages(c("choroplethr", "choroplethrMaps", "tidycensus", "tigris", "leaflet", "acs", "sf"))

library(choroplethr)
library(choroplethrMaps)
library(tidycensus)
library(tigris)
library(leaflet)
library(acs)
library(sf)
library(tidyverse)

api.key.install("my_api_key")

SD_geo <- geo.make(state="CA", county = 73, tract = "*", block.group = "*")

median_income <- acs.fetch(endyear = 2015, span = 5, geography = SD_geo, table.number = "B19013", col.names="pretty")

当我收到以下错误消息时,一切似乎都可以正常工作,直到最后的命令:

trying URL 'http://web.mit.edu/eglenn/www/acs/acs-variables/acs_5yr_2015_var.xml.gz'
Content type 'application/xml' length 735879 bytes (718 KB)
downloaded 718 KB

Error in if (url.test["statusMessage"] != "OK") { : 
  missing value where TRUE/FALSE needed
In addition: Warning message:
In (function (endyear, span = 5, dataset = "acs", keyword, table.name,  :
  XML variable lookup tables for this request
  seem to be missing from ' https://api.census.gov/data/2015/acs5/variables.xml ';
  temporarily downloading and using archived copies instead;
  since this is *much* slower, recommend running
  acs.tables.install()

这让我感到困惑,因为 1) 看起来好像一开始实际上正在下载某些东西?和 2) 'if (url.test["statusMessage"] != "OK") { : 需要 TRUE/FALSE 的缺失值' 对我来说没有意义。它与函数中的任何参数都不对齐。

我努力了:

  • 按照错误消息后半部分的建议,使用 acs.tables.install() 下载表。没有帮助。

  • 更改 endyear 和 span 以确保我属于 API 支持的数据年份。根据 API 文档,我似乎是。也没有运气使用包默认参数。

  • 使用 'variable =' 和官方 API 文档中的变量代码。这仅返回带有神秘“如果...中的错误”消息的两行。

  • 删除 colnames = "pretty"

我现在只是将数据文件下载为 CSV 并将其读入 R,但我希望能够从脚本中为未来的地图执行此功能。任何有关这里发生的事情的信息将不胜感激。我正在运行 R 版本 3.3.2。另外,我不熟悉使用这个包和 API。但我正在关注文档,找不到证据证明我做错了什么。

我正在处理的教程:http: //zevross.com/blog/2015/10/14/manipulating-and-mapping-us-census-data-in-r-using-the-acs-tigris-and-leaflet -packages-3/#get-the-tabular-data-acs

以及 acs 包的文档: http: //eglenn.scripts.mit.edu/citystate/wp-content/uploads/2013/02/wpid-working_with_acs_R2.pdf

4

3 回答 3

2

为了跟进 Brandon 的评论,该软件包的 2.1.1 版现在在 CRAN 上,应该可以解决这个问题。

于 2017-08-08T15:46:58.537 回答
0

你的代码为我运行。我的猜测是人口普查 API 暂时关闭了。

当您加载tidycensus并且想要进行一些映射时,您可能还需要考虑以下代码:

library(tidycensus)
census_api_key("your key here") # use `install = TRUE` to install the key
options(tigris_use_cache = TRUE) # optional - to cache the Census shapefile

median_income <- get_acs(geography = "block group", 
                         variables = "B19013_001", 
                         state = "CA", county = "San Diego", 
                         geometry = TRUE)

这将为您提供所需的数据,以及用于映射的要素几何,作为一个整洁的数据框。

于 2017-07-05T18:57:24.243 回答
0

我给包裹的作者 Ezra Haber Glenn 发了电子邮件,关于这个问题,因为我遇到了同样的问题。我在 30 分钟内收到了回复,那是在午夜之后,我觉得这太棒了。长话短说,acs 包版本 2.1.0 被配置为与人口普查局今年夏天晚些时候对其 API 进行的更改一起使用,同时它目前正在给 Windows 用户带来一些问题。Ezra 将发布一个带有修复程序的更新,但同时我恢复到 2.0 版并且它工作正常。我确信有几种方法可以做到这一点,但我安装了 devtools 包并运行:

需要(开发工具)

install_version("acs", version = "2.0", repos = " http://cran.us.r-project.org ")

希望这可以帮助其他有类似问题的人。

于 2017-07-20T04:34:36.407 回答