Well, consider this code:
What it does it:
- creates
start and end objects in {sf} package data format
- prepares a {leaflet} overview as a sanity check - do both data points look about right?
- creates
distance column in your end object; it contains all the information you provided + distance from the common origin
library(sf)
library(dplyr)
library(leaflet)
start <- data.frame(lat = c(22.41235),
lon = c(92.38767)) %>%
st_as_sf(coords = c("lon", "lat"), crs = 4326)
end <- structure(list(ID = c("ID1", "ID1", "ID1", "ID1", "ID1", "ID1",
"ID1", "ID1", "ID1", "ID1", "ID1", "ID1", "ID1", "ID1", "ID1",
"ID1", "ID1", "ID1", "ID1", "ID1", "ID1", "ID1", "ID1", "ID1",
"ID1", "ID1", "ID1", "ID1", "ID1", "ID1", "ID1", "ID1", "ID1",
"ID1", "ID1", "ID1", "ID1", "ID1", "ID1", "ID1", "ID1", "ID1",
"ID1", "ID1", "ID1", "ID1", "ID1", "ID1", "ID1", "ID1", "ID2",
"ID2", "ID2", "ID2", "ID2", "ID2", "ID2", "ID2", "ID2", "ID2",
"ID2", "ID2", "ID2", "ID2", "ID2", "ID2", "ID2", "ID2", "ID2",
"ID2", "ID2", "ID2", "ID2", "ID2", "ID2", "ID2", "ID2", "ID2",
"ID2", "ID2", "ID2", "ID2", "ID2", "ID2", "ID2", "ID2", "ID2",
"ID2", "ID2", "ID2", "ID2", "ID2", "ID2", "ID2", "ID2", "ID2",
"ID2", "ID2", "ID2", "ID2"), Latitude = c(22.15733, 22.1508,
22.152249, 22.15686, 22.1508, 22.15184, 22.1521, 22.15516, 22.15137,
22.15838, 22.15078, 22.15932, 22.15942, 22.15937, 22.15732, 22.15527,
22.15733, 22.15148, 22.15244, 22.15702, 22.15511, 22.15876, 22.15866,
22.1557, 22.15813, 22.16191, 22.16227, 22.16226, 22.16121, 22.16535,
22.16563, 22.16495, 22.16256, 22.1625, 22.16497, 22.16335, 22.16464,
22.16512, 22.16542, 22.16342, 22.16568, 22.1635, 22.16104, 22.16323,
22.162557, 22.16102, 22.16245, 22.16227, 22.162321, 22.1614,
22.22376, 22.24522, 22.24535, 22.24435, 22.22383, 22.22381, 22.22362,
22.22351, 22.22343, 22.2234, 22.22359, 22.22376, 22.22391, 22.22401,
22.22384, 22.22348, 22.22711, 22.22711, 22.22681, 22.22649, 22.22692,
22.22708, 22.22656, 22.2266, 22.22708, 22.22718, 22.22744, 22.22727,
22.22391, 22.22448, 22.22446, 22.22624, 22.22674, 22.22651, 22.22611,
22.22617, 22.22599, 22.22418, 22.22637, 22.22424, 22.22392, 22.24445,
22.24411, 22.24395, 22.2442, 22.24396, 22.24539, 22.24363, 22.24279,
22.24419), Longtitude = c(92.3299, 92.32269, 92.32289, 92.33346,
92.32235, 92.3223, 92.3223, 92.32631, 92.32234, 92.3337, 92.324697,
92.33263, 92.33263, 92.33263, 92.32956, 92.33215, 92.3299, 92.32248,
92.3249, 92.33222, 92.33203, 92.3337, 92.33392, 92.33013, 92.33112,
92.34243, 92.34205, 92.34244, 92.34371, 92.34028, 92.3406, 92.34003,
92.34324, 92.34367, 92.33984, 92.34328, 92.34059, 92.37019, 92.34018,
92.34394, 92.34015, 92.34155, 92.34337, 92.34173, 92.34326, 92.3428,
92.34368, 92.34305, 92.34261, 92.34261, 92.27397, 92.28837, 92.28838,
92.28864, 92.27408, 92.27402, 92.27392, 92.27376, 92.27366, 92.27357,
92.27393, 92.27399, 92.27391, 92.27393, 92.27383, 92.27374, 92.27608,
92.27593, 92.27559, 92.27577, 92.27545, 92.27533, 92.27532, 92.27539,
92.27557, 92.27568, 92.27562, 92.27553, 92.18207, 92.18164, 92.18217,
92.18157, 92.18096, 92.18117, 92.18168, 92.18162, 92.18154, 92.1817,
92.18175, 92.1821, 92.18213, 92.28967, 92.2891, 92.28899, 92.28915,
92.2884, 92.28833, 92.28823, 92.288989, 92.28898)), class = "data.frame", row.names = c(NA,
-100L)) %>%
st_as_sf(coords = c("Longtitude", "Latitude"), crs = 4326)
# sanity check - do the points look all right?
leaflet() %>%
addProviderTiles("CartoDB.Positron") %>%
addCircleMarkers(data = start,
fillColor = "red",
stroke = F) %>%
addCircleMarkers(data = end,
fillColor = "blue",
stroke = F)
# calculate the distance
end$distance <- st_distance(start, end, by_element = T)
# check result
head(end)
Simple feature collection with 6 features and 2 fields
geometry type: POINT
dimension: XY
bbox: xmin: 92.3223 ymin: 22.1508 xmax: 92.33346 ymax: 22.15733
geographic CRS: WGS 84
ID geometry distance
1 ID1 POINT (92.3299 22.15733) 28860.15 [m]
2 ID1 POINT (92.32269 22.1508) 29726.61 [m]
3 ID1 POINT (92.32289 22.15225) 29565.64 [m]
4 ID1 POINT (92.33346 22.15686) 28837.77 [m]
5 ID1 POINT (92.32235 22.1508) 29734.53 [m]
6 ID1 POINT (92.3223 22.15184) 29623.53 [m]