如果您正在尝试获取 Red Bank 县分区或 Red Bank内的人口普查区的数据,我并不完全清楚。在任何一种情况下,您都不能直接在 中执行此操作tidycensus
,而是可以使用获取县中的所有细分或区域get_acs()
,然后进一步过滤结果。
例如,如果您只需要 Red Bank 县细分的语言数据,您可以这样做:
library(tidycensus)
library(tidyverse)
library(sf)
library(tigris)
vars <- c(English = "C16002_002",
Spanish = "C16002_003")
# get all subdivisions in monmouth county
language_subdiv <- get_acs(geography = "county subdivision",
state = "NJ",
county = "Monmouth",
table = "C16001")
# only red bank borough
language_subdiv %>%
filter(str_detect(NAME, "Red Bank"))
#> # A tibble: 38 x 5
#> GEOID NAME variable estimate moe
#> <chr> <chr> <chr> <dbl> <dbl>
#> 1 34025624… Red Bank borough, Monmouth County, N… C16001_0… 11405 171
#> 2 34025624… Red Bank borough, Monmouth County, N… C16001_0… 7227 451
#> 3 34025624… Red Bank borough, Monmouth County, N… C16001_0… 3789 425
#> 4 34025624… Red Bank borough, Monmouth County, N… C16001_0… 1287 247
#> 5 34025624… Red Bank borough, Monmouth County, N… C16001_0… 2502 435
#> 6 34025624… Red Bank borough, Monmouth County, N… C16001_0… 0 19
#> 7 34025624… Red Bank borough, Monmouth County, N… C16001_0… 0 19
#> 8 34025624… Red Bank borough, Monmouth County, N… C16001_0… 0 19
#> 9 34025624… Red Bank borough, Monmouth County, N… C16001_0… 42 40
#> 10 34025624… Red Bank borough, Monmouth County, N… C16001_0… 0 19
#> # ... with 28 more rows
现在,如果您想要 Red Bank 内的人口普查区,您可以获取 Monmouth 的所有人口普查区,然后用于tigris::places()
获取 Red Bank 的边界,最后过滤人口普查区以仅获取 Red Bank 边界包含的那些区域。
# get all tracts in monmouth county
language_tract <- get_acs(geography = "tract",
variables = vars,
state = "NJ",
county = "Monmouth",
geometry = TRUE,
summary_var = "C16002_001",
output = "wide")
# get geometry of red bank borough
red_bank_place <- places("NJ", cb = TRUE, class = "sf") %>%
filter(NAME == "Red Bank")
# only tracts in red bank borough
red_bank_tracts <- language_tract %>%
filter(st_contains(red_bank_place, ., sparse = FALSE))
ggplot() +
geom_sf(data = red_bank_tracts, color = "blue", fill = NA) +
geom_sf(data = red_bank_place, color = "black", fill = NA)

由reprex 包(v0.2.1)于 2018 年 12 月 24 日创建