首页 > 解决方案 > 计算动物的每日最大位移

问题描述

我是 R 的新手,需要一些帮助。我有以下几列的动物运动数据:“ TagID”、“ Date”、“ location.long”、“ location.lat

我需要分别找到每天每个人的连续 GPS 点之间距离。之后,我试图找到当天的最大位移t,这意味着当天的第一个点与同一天该动物的最远点之间的距离。

这是数据结构

    TagID   Date      location.long location.lat
1   6634 2018-10-22     10.311283     48.20341
2   6634 2018-10-22     10.319261     48.18381
3   6634 2018-10-22     10.321538     48.15845
4   6634 2018-10-22     10.293819     48.15497
5   6634 2018-10-22     10.369189     48.12718
6   6634 2018-10-22     10.333642     48.14402
7   6634 2018-10-23     10.256952     48.12419
8   6634 2018-10-23     10.283222     48.11557
9   6634 2018-10-23     10.264805     48.10601
10  6634 2018-10-23     10.358082     48.07012
11  6634 2018-10-23     10.310423     48.03421
12  6678 2018-10-22      9.981601     48.24799
13  6678 2018-10-22      9.999586     48.22714
14  6678 2018-10-22      9.963930     48.29394
15  6678 2018-10-22      9.989562     48.17918

到目前为止,我已经达到了这一点:

mydata <- read.csv("tracking.csv")
onlcoor <- subset (mydata, select = c("location.long","location.lat"))
m <- as.matrix(onlcoor)
#Creates a matrix witht the distances between each point in the dataset
mydata$dist <- distm(m, fun=distVincentyEllipsoid)
#Returns the maximum distance between the first point the farthest from it
mydata$displacement <- max(mydata$dist[,1])

它计算连续点之间的距离并返回位移(第一个点和最远点之间的距离)。

现在,我想在代码中添加两个条件

  1. 不考虑 TagID。因此,当距离应为 NA 时,将使用新动物的第一个位置生成动物最后位置的距离。
  2. 日期也应该考虑在内,因为我需要每个人的最大每日位移

这是最终对我有用的代码:

mydata <- read.csv("tracking.csv")    
mydata <- split(mydata, list(mydata$TagID, mydata$Date), drop = TRUE)
displacem <- sapply(mydata, function(mydata){
     onlcoor <- subset (mydata, select = 
c("location.long","location.lat"))
     m <- as.matrix(onlcoor)
     mydata$dist <- distm(m, fun=distVincentyEllipsoid)
     mydata$displ <- max(mydata$dist[,1]) 
   })
write.table(displacem, file = "maximum_displacement.csv",row.names=FALSE, 
na="",col.names=FALSE, sep=",")

标签: rgps

解决方案


推荐阅读