使用 R 使用 sf 确定哪些县在较大的国会选区内

Use R to identify which counties are within larger congressional districts using sf

我有一个关于美国县的 sf 对象,还有一个关于美国国会选区的对象。我需要知道 (i) 哪些县在哪个国会选区,以及 (ii) 如果一个县与选区边界重叠(即包含在两个选区内),那么我需要知道它在哪个选区“更多”,或者它的比例是多少每个县都有。

这是我的确切 sf 对象:

library(USAboundaries)
library(sf)

union_states <- c("Maine", "New Hampshire", "Vermont", "New York", "Massachusetts", "Rhode Island", "Connecticut", "Pennsylvania", "New Jersey", "Ohio", "Indiana", "Illinois", "Iowa", "Wisconsin", "Minnesota", "Michigan") # only core states: exluces CA, WA, KS, and boundary states
union_sf <- us_counties(map_date = "1865-01-01", states = union_states, resolution = 'high')
union_congress_sf <- us_congressional(resolution = "low", states = union_states)

这个问题提出了我的确切问题,但有点过时并且不适用于 sf 对象:

您可以将 st_join()largest = TRUE 参数一起使用。

对于县和国会选区对象,我删除了大部分列,因此结果更清晰。在输出中,name 是县,geoid 是国会选区。

library(dplyr)

union_sf <- union_sf %>% 
  select(name, geometry)
  
union_congress_sf <- union_congress_sf %>% 
  select(geoid, state_name, geometry)

join <- st_join(union_sf,
                union_congress_sf,
                largest = TRUE)

join

Simple feature collection with 804 features and 3 fields
geometry type:  MULTIPOLYGON
dimension:      XY
bbox:           xmin: -97.23421 ymin: 36.97353 xmax: -66.94993 ymax: 49.38437
geographic CRS: WGS 84
First 10 features:
           name geoid  state_name                       geometry
2455  FAIRFIELD  0904 Connecticut MULTIPOLYGON (((-73.5055 41...
2481   HARTFORD  0901 Connecticut MULTIPOLYGON (((-72.81354 4...
2495 LITCHFIELD  0905 Connecticut MULTIPOLYGON (((-73.00875 4...
2497  MIDDLESEX  0902 Connecticut MULTIPOLYGON (((-72.52454 4...
2541  NEW HAVEN  0903 Connecticut MULTIPOLYGON (((-72.93861 4...
2554 NEW LONDON  0902 Connecticut MULTIPOLYGON (((-72.33685 4...
2560    TOLLAND  0902 Connecticut MULTIPOLYGON (((-72.10217 4...
2569    WINDHAM  0902 Connecticut MULTIPOLYGON (((-71.79924 4...
4443      ADAIR  1903        Iowa MULTIPOLYGON (((-94.24152 4...
4444      ADAMS  1903        Iowa MULTIPOLYGON (((-94.47062 4...