Please tell me, this question is derived from the previous topic. how to subtract coordinates by formula in R
there are 2 datasets
i=structure(list(State = c("x1", "x2", "x3", "x4", "x5", "x6",
"x7", "x8", "x9", "x10"), centroid_lat = c(25.0567526488506,
24.5506057345197, 25.108046481826, 25.0238523142178, 24.994141005348,
25.0811976922289, 24.9630616206443, 24.9924361621629, 25.2103128046815,
25.2173817704152), centroid_lon = c(76.5385899060845, 76.7828927388028,
76.4028924220128, 76.4542417907297, 76.4114079037918, 76.4363750139148,
76.4794068845473, 76.4328442796457, 76.5591513892293, 76.600503986722
), fin = c(3219L, 1519L, 2184L, 1919L, 3003L, 2040L, 1843L, 3502L,
1085L, 2215L), fin2 = c(3789L, 1452L, 2420L, 1971L, 1719L, 2006L,
1463L, 1278L, 1351L, 3163L), fin3 = c(1513L, 1740L, 2115L, 2431L,
1148L, 2251L, 1370L, 2122L, 2679L, 1999L), fin4 = c(1903L, 1891L,
1687L, 1301L, 3080L, 2713L, 2006L, 3891L, 2537L, 1145L)), class = "data.frame", row.names = c(NA,
-10L))
second
k=structure(list(State = c("x11", "x12", "x13", "x14", "x15", "x16",
"x17", "x18", "x19", "x20"), centroid_lat = c(24.05675265, 23.55060573,
24.10804648, 24.02385231, 23.99414101, 35.08119769, 34.96306162,
34.99243616, 35.2103128, 35.21738177), centroid_lon = c(75.53858991,
75.78289274, 75.40289242, 75.45424179, 75.4114079, 86.43637501,
86.47940688, 86.43284428, 86.55915139, 86.60050399), fin1 = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA), fin2 = c(NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA), fin3 = c(NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA), fin4 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)), class = "data.frame", row.names = c(NA,
-10L))
Each file has a state column, in fact x1-x20 is just the name of the villages, just to avoid confusion, I coded them like this.
File i has metric variables fin1-fin4.
There are these columns in file k, but they are empty, there is no data on them, I want to substitute these data.
One of my ideas is to calculate the distances between coordinates, find the nearest 5 coordinates of villages from file i to some village to village from file k.
Let's say the coordinates to the village x11 from the file k the closest coordinates of the villages x1, x2, x4, x5, x7 from i file.
We look at the data on the variables fin1-fin4 for these 5 villages, average and insert the resulting averages into the columns fin1-fin4 for the village x11.
Similarly, we take the village x12 from the file к look for the closest 5 coordinates to it from the file i , and also average the values and insert the resulting averages into the columns fin1-fin4 for the village x12
How to make the difference in coordinates was suggested in my last topic above by the answer of @Allan Cameron
library(sf)
i_sf <- sf::st_as_sf(i, coords = c('centroid_lon', 'centroid_lat'), crs = 4326)
k_sf <- sf::st_as_sf(k, coords = c('centroid_lon', 'centroid_lat'), crs = 4326)
sf::st_distance(i_sf, k_sf)
but how to find the nearest 5 coordinates and substitute the average in the к file for each village with x 11 x 20 respectively.
Thanks for any valuable help.