I am trying to merge the two dataframes (main and sub). I want the 'variable' data from 'sub' to be merged to 'main' based on distance or better yet, whichever 'sub' row/site is closest to the 'main' row/site.
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)
I believe that the st_intersection is what I want, but if I could do it one-to-one based on distance, that would make it work. Does anyone know what could provide the result I am looking for?