I have a data frame [df] like this:
df<-structure(list( Latitude = c(-23.8, -23.8, -23.9, -23.9),
Longitude = c(-49.6, -49.3, -49.4, -49.8),
Latitude1 = c(-23.4, -23.7, -23.4, -23.8),
Longitude1 = c(-49.7, -49.4, -49.6, -49.7)),
class = "data.frame", row.names = c(NA, -4L))
The dataframe contains the GPS coordinates of 2 points and I would like to calculate the distance in meters between these 2 points in every row. I would only like to get the distance between the 2 points in each row, not a distance matrix.
The desired result would look like this:
Latitude Longitude Latitude1 Longitude1 Distance_m
-23.8 -49.6 -23.4 -49.7 53
I tried the geosphere package, but I was not able to get the right results.
Is there any way of doing this, please?
Thank you for any suggestions.
Check the distm
function from geosphere
package:
apply(df, 1, function(x)distm(c(x[1],x[2]),c(x[3],x[4]),fun = distGeo))
withing the tidyverse
// could not reproduce your desired output of 53 though... ?
library(geosphere)
library(tidyverse)
df %>%
mutate(distance = pmap(list(a = Longitude,
b = Latitude,
x = Longitude1,
y = Latitude1),
~ geosphere::distGeo( c(..1, ..2), c(..3, ..4))))
# Latitude Longitude Latitude1 Longitude1 distance
# 1 -23.8 -49.6 -23.4 -49.7 45461.49
# 2 -23.8 -49.3 -23.7 -49.4 15053.19
# 3 -23.9 -49.4 -23.4 -49.6 59016.34
# 4 -23.9 -49.8 -23.8 -49.7 15048.01
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.