您正在寻找空间连接。由于您的数据是纬度/经度,您需要投影到邻域坐标,以便您的数据对齐。在处理度量单位时,经度/纬度不是很好。
然后,您需要使用 st_within 对点进行空间连接,如果属性落在邻域多边形内,则它们将合并。从那里您可以将列子集化为所需的输出。
请注意,默认为左连接,因此请确保您的数据框位于第一位。您没有提供任何数据,所以我不知道这是否 100% 适用于您数据中的所有案例。
编辑:我发现该站点的社区 shapefile 有一些无效的几何图形。你可能想通过st_make_valid() 来解决这个问题。我还添加了一个包含一些虚拟数据和邻域 shapefile 的示例。重复并不重要,您可以随心所欲地处理它们。
library(sf)
# read in neighborhoods shapefile
neighborhoods <- st_read('Boston_Neighborhoods.shp')%>%
st_make_valid()
# convert df to spatial dataframe and reproject
spdf <- st_as_sf(df, coords=c("Longitude", "Latitude"), crs=st_crs("EPSG:4326")) %>%
st_transform(crs=st_crs(neighborhoods))
# do the spatial join
output <- st_join(spdf, neighborhoods, join=st_within)
这是一个包含一些虚拟数据的示例。
plist <- as.data.frame(do.call(rbind, list(c(756363.463294525, 2926358.57093617),
c(756363.463294525, 2926358.57093617),
c(761610.797283232, 2937282.14778728),
c(763204.448665894, 2946825.83819385),
c(763085.235486707, 2950007.83174181))))
pts <- st_as_sf(plist,
crs=st_crs(neighborhoods),
coords=c(1,2))
st_join(pts, neighborhoods, join=st_within)
# Simple feature collection with 5 features and 7 fields
# geometry type: POINT
# dimension: XY
# bbox: xmin: 756363.5 ymin: 2926359 xmax: 763204.4 ymax: 2950008
# projected CRS: NAD83 / Massachusetts Mainland (ftUS)
# OBJECTID Name Acres Neighborho SqMiles ShapeSTAre ShapeSTLen geometry
# 1 27 Roslindale 1605.5682 15 2.51 69938273 53563.91 POINT (756363.5 2926359)
# 2 27 Roslindale 1605.5682 15 2.51 69938273 53563.91 POINT (756363.5 2926359)
# 3 28 Jamaica Plain 2519.2454 11 3.94 109737890 56349.94 POINT (761610.8 2937282)
# 4 29 Mission Hill 350.8536 13 0.55 15283120 17918.72 POINT (763204.4 2946826)
# 5 30 Longwood 188.6119 28 0.29 8215904 11908.76 POINT (763085.2 2950008)