0

我正在尝试将“县细分”的数据作为 tidycensus 的 get_acs() 函数中地理选项的一部分上传。我知道有几个地理选项,凯尔沃克在他的页面上发布了这些选项。https://walkerke.github.io/tidycensus/articles/basic-usage.html#geography-in-tidycensus

虽然它适用于州和县级,因为您只需输入县 =“蒙茅斯”。但我似乎无法让语法在蒙茅斯县内的城市的城市细分级别上工作。我寻找了其他 tidycensus 脚本,但没有找到任何使用县级以下的地理。

有什么建议么?

library(tidycensus)
library(tidyverse)
library(sf)

census_api_key("YOUR API KEY GOES HERE")

vars <- c(English = "C16002_002", 
      Spanish = "C16002_003")


language <- get_acs(geography = "county subdivision", 
                state = "NJ",
                county = "Monmouth",
                city = "Red Bank",
                table = "C16001")



rb_language <- get_acs(geography = "tract", 
                   variables = vars,
                   state = "NJ", 
                   county = "Monmouth", 
                   city = "Red Bank"
                   geometry = TRUE, 
                   summary_var = "C16002_001") %>%
  st_transform(26918)
4

1 回答 1

0

如果您正在尝试获取 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 日创建

于 2018-12-24T14:19:26.347 回答