I have two data set of different stations. The data are basically data.frames with coordinates, longitudes and latitudes. Given the first data set (or vice versa), I want to
The function s2_closest_feature() from the s2 package finds nearest points from different data sets.
For example, with your data:
library(s2)
set1_s2 <- s2_lnglat(set1$lon, set1$lat)
set2_s2 <- s2_lnglat(set2$lon, set2$lat)
set1$closest <- s2_closest_feature(set1_s2, set2_s2)
set1
#> lon lat closest
#> 1 13.67111 48.39167 10
#> 2 12.86695 48.14806 10
#> 3 15.94223 48.72111 10
#> 4 11.09974 47.18917 1
#> 5 12.95834 47.05444 1
#> 6 14.20389 47.12917 1
#> 7 11.86389 47.30667 1
#> 8 16.52667 47.84000 1
#> 9 16.19306 47.30417 1
#> 10 17.07139 48.10944 1