我正在尝试合并两个数据框(主要和子)。我希望从“子”中获取“变量”数据,并基于距离或最接近“主要”行/站点的任何“子”行/站点将其合并到“主要”中。
library(sf)
a <- structure(list(`Site#` = c("Site1", "Site2", "Site3", "Site4", "Site5", "Site6"), Longitude = c(-94.609, -98.1391, -99.033, -98.49, -96.4309, -95.99), `Latitude` = c(38.922, 37.486111, 37.811, 38.364, 39.4402, 39.901)), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"))
main <- st_as_sf(a, coords = c("Longitude", "Latitude"), crs = 4326)
b <- structure(list(Longitude = c(-98.49567, -96.22451, -98.49567, -98.941391, -95.91411, -99.031113), `Latitude` = c(38.31264,39.97692, 38.31264, 37.486111, 39.92143, 37.814171), Variable = c(400, 50, 100, 201, 99, 700)), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"))
sub <- st_as_sf(b, coords = c("Longitude", "Latitude"), crs = 4326)
c <- st_intersection(main,sub)
c <- st_is_within_distance(main,sub,dist=0.001)
我相信 st_intersection 是我想要的,但如果能基于距离进行一对一的匹配,那会更好。有人知道能提供我想要结果的方法吗?