Search code examples
rgps

In R, how to sum the distance between rows (each one is a GPS coordinate)?


I have many GPS points and what I want is the sum of distances between two subsequent points (rows) in a given date so I can have a daily track distance.

Each day has about 200 GPS points. Subsequent points means two rows in which the first is earlier than the second. Because I need the total distance among these points, it must take into consideration the row order of the column "time" within a given day (column "date").

Thank you!

My table sorta looks like this:

date        time       lat        lon    
18-Jan-18 12:48:39 -24.061464 -47.99523
18-Jan-18 12:48:48 -24.06163  -47.995354
18-Jan-18 12:53:17 -24.06175  -47.995277

Solution

  • We can use pointDistance from the package raster to calculate the distance. lag from dplyr will help calculating on subsequent points. replace_na from tidyr is very convenient, but you could use your favorite way of dealing with NA.

    library(raster)
    library(dplyr)
    library(tidyr)
    data %>% 
      mutate(Distance = pointDistance(cbind(lon,lat),cbind(lag(lon),lag(lat)),lonlat = TRUE)) %>%
      mutate(TotalDistance = cumsum(replace_na(Distance,0)))
    #       date     time       lat       lon Distance TotalDistance
    #1 18-Jan-18 12:48:39 -24.06146 -47.99523       NA       0.00000
    #2 18-Jan-18 12:48:48 -24.06163 -47.99535 22.29547      22.29547
    #3 18-Jan-18 12:53:17 -24.06175 -47.99528 15.42660      37.72207  
    

    Data

    data <- structure(list(date = structure(c(1L, 1L, 1L), .Label = "18-Jan-18", class = "factor"), 
        time = structure(1:3, .Label = c("12:48:39", "12:48:48", 
        "12:53:17"), class = "factor"), lat = c(-24.061464, -24.06163, 
        -24.06175), lon = c(-47.99523, -47.995354, -47.995277)), class = "data.frame", row.names = c(NA, 
    -3L))