Calculate latitude longitude of multiplicity in data frame R

I am not sure if this is the right place to ask my question (I am new to R and to this site). My question is this: how can I calculate the distance between the longitude and latitude points?

I searched on this site for an answer to my problem, but the answers are considered only for 2 points (while I have a data set containing more than 207,000 rows).

I have a dataframe called adsb_relevant_columns_correct_timedifference containing the following columns: Callsign, Altitude, Speed, Direction, Date_Time, Latitude, Longitude.

Callsign Altitude  Speed Direction   Date_Time             Latitude     Longitude
A118       18000    110  340         2017-11-06 22:28:09   70.6086      58.2959
A118       18500    120  339         2017-11-06 22:29:09   72.1508      58.7894
B222       18500    150  350         2017-11-08 07:28:09   71.1689      59.1234
D123       19000    150  110         2018-05-29 15:13:27   69.4523      68.1235

I would like to calculate the distance (in meters) between each dimension (each row is a new dimension) and add this as a new “Distance” column. This first distance calculation should appear in the second row, because for subsequent purposes. Therefore, the first row of the Distance column can be zero or NA, which does not matter.

So, I would like to know the distance between the first dimension (Lat 70.6086 and Long 58.2959) and the second dimension (Lat 72.1508 and 58.7894). Then the distance between the second and third dimensions. Then between the third and fourth, etc. For more than 207000 measurements.

The expected output should look like this:

Callsign Altitude  Speed Direction   Date_Time             Latitude     Longitude  Distance 
A118       18000    110  340         2017-11-06 22:28:09   70.6086      58.2959    NA  
A118       18500    120  339         2017-11-06 22:29:09   72.1508      58.7894    172000
B222       18500    150  350         2017-11-08 07:28:09   71.1689      59.1234    110000
D123       19000    150  110         2018-05-29 15:13:27   69.4523      68.1235    387000

I found the distm function in R, for which I can do this manually for only two dimensions, instead of a complete dataset.

distm(p1, p2, fun = distHaversine)

I tried the following code

adsb_relevant_columns_correct_timedifference <- mutate(adsb_relevant_columns_correct_timedifference, Distance =
distm(c(adsb_relevant_columns_correct_timedifference$Longitude, adsb_relevant_columns_correct_timedifference$Latitude),
      c(lag(adsb_relevant_columns_correct_timedifference$Longitude, adsb_relevant_columns_correct_timedifference$Latitude)), fun = distCosine))

However, I got the following error

mutate_impl (.data, dots):    : 2.

, , . -, , , ?

+4
4

distm distHaversine -. mutate $, mutate , . , cbind c, c , , cbind ( ).

library(geosphere)
library(dplyr)

mutate(mydata, 
       Distance = distHaversine(cbind(Longitude, Latitude),
                                cbind(lag(Longitude), lag(Latitude))))

#   Callsign Altitude Speed Direction           Date_Time Latitude Longitude Distance
# 1     A118    18000   110       340 2017-11-06T22:28:09  70.6086   58.2959       NA
# 2     A118    18500   120       339 2017-11-06T22:29:09  72.1508   58.7894 172569.2
# 3     B222    18500   150       350 2017-11-08T07:28:09  71.1689   59.1234 109928.5
# 4     D123    19000   150       110 2018-05-29T15:13:27  69.4523   68.1235 387356.2

distCosine , NA, . , , :

modified_distCosine <- function(Longitude1, Latitude1, Longitude2, Latitude2) {
  if (any(is.na(c(Longitude1, Latitude1, Longitude2, Latitude2)))) {
    NA
  } else {
    distCosine(c(Longitude1, Latitude1), c(Longitude2, Latitude2))
  }
}

mutate(mydata, 
       Distance = mapply(modified_distCosine, 
                         Longitude, Latitude, lag(Longitude), lag(Latitude)))

#   Callsign Altitude Speed Direction           Date_Time Latitude Longitude Distance
# 1     A118    18000   110       340 2017-11-06T22:28:09  70.6086   58.2959       NA
# 2     A118    18500   120       339 2017-11-06T22:29:09  72.1508   58.7894 172569.2
# 3     B222    18500   150       350 2017-11-08T07:28:09  71.1689   59.1234 109928.5
# 4     D123    19000   150       110 2018-05-29T15:13:27  69.4523   68.1235 387356.2

mapply Longitude, Latitude, lag(Longitude), lag(Latitude).
, , , , .

mydata <- structure(list(Callsign = c("A118", "A118", "B222", "D123"), 
                         Altitude = c(18000L, 18500L, 18500L, 19000L), 
                         Speed = c(110L, 120L, 150L, 150L), 
                         Direction = c(340L, 339L, 350L, 110L), 
                         Date_Time = c("2017-11-06T22:28:09", "2017-11-06T22:29:09", "2017-11-08T07:28:09", "2018-05-29T15:13:27"), 
                         Latitude = c(70.6086, 72.1508, 71.1689, 69.4523), 
                         Longitude = c(58.2959, 58.7894, 59.1234, 68.1235)), 
                    .Names = c("Callsign", "Altitude", "Speed", "Direction", "Date_Time", "Latitude", "Longitude"), 
                    class = "data.frame", row.names = c(NA, -4L))
+1

distm . :

library(geosphere)
p <- cbind(df$Longitude, df$Latitude)
distm(head(p, -1), tail(p, -1), fun = distHaversine)
#          [,1]     [,2]     [,3]
# [1,] 172569.2  69279.8 394651.3
# [2,]      0.0 109928.5 454096.2
# [3,] 109928.5      0.0 387356.2

diag(distm(head(p, -1), tail(p, -1), fun = distHaversine))
# [1] 172569.2 109928.5 387356.2

distHaversine :

distHaversine(head(p, -1), tail(p, -1))
# [1] 172569.2 109928.5 387356.2

df$Distance <- c(NA, distHaversine(head(p, -1), tail(p, -1)))
+1

library(geosphere)
df <- data.frame(read.table(text ="
Callsign Altitude  Speed Direction   Date_Time             Latitude     Longitude
A118       18000    110  340         2017-11-06T22:28:09   70.6086      58.2959
A118       18500    120  339         2017-11-06T22:29:09   72.1508      58.7894
B222       18500    150  350         2017-11-08T07:28:09   71.1689      59.1234
D123       19000    150  110         2018-05-29T15:13:27   69.4523      68.1235"
, header=TRUE))

, .

df$distance[2:nrow(df)] <- sapply(2:nrow(df), 
    function(x) distm(df[x-1,c('Longitude', 'Latitude')], df[x,c('Longitude', 'Latitude')], fun = distHaversine))

df
#  Callsign Altitude Speed Direction           Date_Time Latitude Longitude distance
#1     A118    18000   110       340 2017-11-06T22:28:09  70.6086   58.2959       NA
#2     A118    18500   120       339 2017-11-06T22:29:09  72.1508   58.7894 172569.2
#3     B222    18500   150       350 2017-11-08T07:28:09  71.1689   59.1234 109928.5
#4     D123    19000   150       110 2018-05-29T15:13:27  69.4523   68.1235 387356.2
0

sf st_distance(). , - . ( , data - data.frame.

library('sf')    

# some points
poi <- data.frame(id = 1:4, 
                  long = c(103.864325, 103.874916, 103.989693, 103.789615), 
                  lat = c(1.281949, 1.305004, 1.359878, 1.404454),
                  name = c('Gardens by the Bay', 'National Stadium', 'Airport', 'Zoo'))

# coerce to sf object
poi <- st_as_sf(x = poi, coords = c('long', 'lat'))

# duplicate object with shifted rows
poi2 <- poi[c(1, 1:(nrow(poi) - 1)), ]

# compute pairwise distance
distance <- st_distance(x = poi, y = poi2)

# extract only the diagonal corresponding to the distance between row r and row r+1
distance <- diag(distance)

# add result to data
poi$distance <- distance

# first distance to NA
poi$distance[1] <- NA
0

Source: https://habr.com/ru/post/1695464/


All Articles