有几个R软件包使使用美国人口普查数据更容易。我最常使用的两个是tigris(用于加载空间数据)和acs(用于加载表格数据)。
但是,我一直遇到的一个问题是,在不离开R控制台的情况下,我想不出一种有效,可靠的方法来确定场所中的所有区域(或街区组,邮政编码等)。
例如,如果我想在西雅图使用人口普查区块数据,则首先使用tigris::tracts下载华盛顿州金县的空间数据:

library(tigris)
tr <- tigris::tracts(state = "WA", county = "King")

但不幸的是,没有明显的方法可以将这些数据进行子集化以仅包含西雅图的区域。
glimpse(tr)
Observations: 398
Variables: 12
$ STATEFP  (chr) "53", "53", "53", "53", "53", "53", "53", ...
$ COUNTYFP (chr) "033", "033", "033", "033", "033", "033", ...
$ TRACTCE  (chr) "003800", "021500", "032704", "026200", "0...
$ GEOID    (chr) "53033003800", "53033021500", "53033032704...
$ NAME     (chr) "38", "215", "327.04", "262", "327.03", "3...
$ NAMELSAD (chr) "Census Tract 38", "Census Tract 215", "Ce...
$ MTFCC    (chr) "G5020", "G5020", "G5020", "G5020", "G5020...
$ FUNCSTAT (chr) "S", "S", "S", "S", "S", "S", "S", "S", "S...
$ ALAND    (dbl) 624606, 3485578, 17160645, 15242622, 10319...
$ AWATER   (dbl) 0, 412526, 447367, 526886, 175464, 0, 4360...
$ INTPTLAT (chr) "+47.6794093", "+47.7643848", "+47.4940877...
$ INTPTLON (chr) "-122.2955292", "-122.2737863", "-121.7717...

同样,acs包允许用户使用geo.make函数创建普查数据的子集,但是在我的示例中,如果我还没有所有西雅图地区的地区地理列表,这将无济于事。
作为记录,我知道可以在其他地方确定此信息。 Census.gov常见问题解答中的page给出了有关如何确定给定普查场所中所有区域的清晰说明。但是,鉴于这是许多与普查相关的分析中的关键步骤,因此最好是从R控制台中找到一种方便的方法来进行。
提前致谢。
编辑
尽管此问题涉及空间数据,但我对寻找非空间解决方案最感兴趣。例如,我更喜欢查询人口普查API并将返回值返回所需GEOID的向量的解决方案,该解决方案采用空间分析工具(例如rgeos::intersects)来创建向量。为什么?因为空间方法在此过程中更容易出错,并且这是我们正在谈论的已知信息,而不是需要在空间上进行推断的信息。

最佳答案

我经常需要相同类型的数据,所以我写了一个R包来完成这项工作。该程序包称为totalcensus。您可以在https://github.com/GL-Li/totalcensus中找到它。

使用此软件包,您可以轻松地在城镇,城市,县,都市区和所有其他地理区域的区域,街区组或街区级别获取数据。例如,如果您想从2011-2015 ACS五年调查中获取各个区域的块组级别的比赛数据,只需运行以下代码即可:

mixed <- read_acs5year(
    year = 2015,
    states = c("ut", "ri"),
    table_contents = c(
        "white = B02001_002",
        "black = B02001_003",
        "asian = B02001_005"
    ),
    areas = c(
        "Lincoln town, RI",
        "Salt Lake City city, UT",
        "Salt Lake City metro",
        "Kent county, RI",
        "COUNTY = UT001",
        "PLACE = UT62360"
    ),
    summary_level = "block group"
)


它返回如下数据:

#                      area               GEOID        lon      lat state population white black asian GEOCOMP SUMLEV                                                             NAME
#    1:    Lincoln town, RI 15000US440070115001  -71.46686 41.94419    RI       1561  1386   128    47     all    150 Block Group 1, Census Tract 115, Providence County, Rhode Island
#    2:    Lincoln town, RI 15000US440070115002  -71.47159 41.96754    RI        916   806    97     0     all    150 Block Group 2, Census Tract 115, Providence County, Rhode Island
#    3:    Lincoln town, RI 15000US440070115003  -71.47820 41.96364    RI       2622  2373    77    86     all    150 Block Group 3, Census Tract 115, Providence County, Rhode Island
#    4:    Lincoln town, RI 15000US440070115004  -71.47830 41.97346    RI       1605  1516    43     0     all    150 Block Group 4, Census Tract 115, Providence County, Rhode Island
#    5:    Lincoln town, RI 15000US440070116001  -71.44665 41.93120    RI        948   764     0     0     all    150 Block Group 1, Census Tract 116, Providence County, Rhode Island
# ---
# 1129: Providence city, UT 15000US490050012011 -111.82424 41.69198    UT       2018  1877     0     0     all    150            Block Group 1, Census Tract 12.01, Cache County, Utah
# 1130: Providence city, UT 15000US490050012012 -111.80736 41.69323    UT       1486  1471     0     0     all    150            Block Group 2, Census Tract 12.01, Cache County, Utah
# 1131: Providence city, UT 15000US490050012013 -111.81310 41.65837    UT       1563  1440    15     0     all    150            Block Group 3, Census Tract 12.01, Cache County, Utah
# 1132: Providence city, UT 15000US490050012022 -111.85231 41.68674    UT       3894  3594     0     0     all    150            Block Group 2, Census Tract 12.02, Cache County, Utah
# 1133: Providence city, UT 15000US490059801001 -111.64525 41.67498    UT        118   118     0     0     all    150             Block Group 1, Census Tract 9801, Cache County, Utah

08-20 00:20