I'm working on an R project involving the analysis of two dataFrames: df
and df
2. The df
contains information about properties grouped into clusters, while df2
contains geographical coordinates of points of interest. My goal is to determine the best location to choose based on the distances between properties within each cluster in df
and all points in df2
. Please, use distm
function. For example, for cluster 1 the best point is id = 3
, for cluster 2 is id = 1
, and so. This is idea
df <- structure(list(ID = c("A", "B", "C", "D", "E", "F", "G"),
Longitude = c(-46.585970687, -51.037570982, -49.656622, -46.730269,
-49.255356686, -48.982128819, -50.177308),
Latitude = c(-23.611080198, -21.659062133, -21.24666, -21.948196,
-22.880896112, -22.429487865, -21.578706),
Cluster = c(1L, 1L, 1L, 2L, 2L, 2L, 2L)),
row.names = c(NA, -7L),
class = c("tbl_df", "data.frame"))
ID Longitude Latitude Cluster
1 A -46.58597 -23.61108 1
2 B -51.03757 -21.65906 1
3 C -49.65662 -21.24666 1
4 D -46.73027 -21.94820 2
5 E -49.25536 -22.88090 2
6 F -48.98213 -22.42949 2
7 G -50.17731 -21.57871 2
df2 <- structure(list(id = c(1, 2, 3, 4, 5, 6, 7, 8), Longitude = c(-52.810111532,
-52.810111532, -52.710111532, -52.710111532, -52.710111532, -52.610111532,
-52.610111532, -52.610111532), Latitude = c(-22.479655795, -22.579655795,
-22.379655795, -22.479655795, -22.579655795, -22.279655795, -22.379655795,
-22.479655795)), row.names = c(NA, -8L), class = c("tbl_df",
"tbl", "data.frame"))
id Longitude Latitude
<dbl> <dbl> <dbl>
1 1 -52.8 -22.5
2 2 -52.8 -22.6
3 3 -52.7 -22.4
4 4 -52.7 -22.5
5 5 -52.7 -22.6
6 6 -52.6 -22.3
7 7 -52.6 -22.4
8 8 -52.6 -22.5