R st_join return 具有点属性的多边形

R st_join return polygon with point attributes

假设我有以下数据和代码。代码 returns point 数据但我想要一个 polygon.

我怎样才能使 spatial join 成为 returns 具有 pointpolygon 属性的 polygon? (基本上,数据将 matched/joined 基于落在多边形中的点)

代码+数据

library(sf)
library(tidyverse)

# Sample poly
poly =  st_read(system.file("shape/nc.shp", package="sf")) # included with sf package

# Sample points
pts = data.frame(name = c("Raleigh", "Greensboro", "Wilmington"),
                  x = c(-78.633333, -79.819444, -77.912222),
                  y = c(35.766667, 36.08, 34.223333)) %>% 
  st_as_sf(coords = c("x", "y"), crs = 4326) %>% 
  st_transform(st_crs(poly)) 

# Spatial join and output a polygon with the joined attributes, stuck here....
cities_with_counties = st_join(pts,
                                poly)

sf::st_join() 返回的几何类型由函数第一个参数驱动。

考虑翻转两者 - st_join(poly, pts)

输出的差异应该仅在于几何类型(和列的排序)。