简体   繁体   English

在R中,如何求和行之间的距离(每行是一个GPS坐标)?

[英]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.我有很多 GPS 点,我想要的是给定日期中两个后续点(行)之间的距离总和,这样我就可以有一个每日的跟踪距离。

Each day has about 200 GPS points.每天大约有200个GPS点。 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

We can use pointDistance from the package raster to calculate the distance.我们可以使用pointDistance raster中的 pointDistance 来计算距离。 lag from dplyr will help calculating on subsequent points. dplyrlag将有助于计算后续点。 replace_na from tidyr is very convenient, but you could use your favorite way of dealing with NA .来自tidyrreplace_na非常方便,但您可以使用自己喜欢的方式处理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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM