我在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])
它计算连续点之间的距离并返回位移(从第一个点到最远点的距离)。
现在,我想在代码中添加两个条件
以下是最终对我有用的代码:
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=",")