r - 计算动物的每日最大位移
问题描述
我是 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])
它计算连续点之间的距离并返回位移(第一个点和最远点之间的距离)。
现在,我想在代码中添加两个条件
- 不考虑 TagID。因此,当距离应为 NA 时,将使用新动物的第一个位置生成动物最后位置的距离。
- 日期也应该考虑在内,因为我需要每个人的最大每日位移
这是最终对我有用的代码:
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=",")
解决方案
推荐阅读
- java - 由于在应用程序属性中使用占位符,无法构建项目
- bootstrap-4 - bootstrap4 错误?reboot.css 覆盖核心引导程序(按钮链接文本黑色)
- wamp - 无法在浏览器上启动 phalcon 项目
- vim - 用 VIM 删除 HTML 标签
- java - java内置的LinkedList类中如何更改节点添加右下指针?
- asp.net - 如果未通过身份验证,则重定向到登录屏幕 - ASP.NET Core 2.2
- postgresql - 更新 postgres 10 中的 jsonb 列
- r - R:具有自定义损失函数的深度神经网络
- ios - iTunes Connect 拒绝存档
- visual-studio - 由于 Microsoft.VisualStudio.Setup.Configuration 包安装失败,无法安装 Visual Studio 2017