我正在计算“同时”记录的 UTM 位置之间的距离(以米为单位),但我遇到了问题。它现在写的方式我只计算只有一个“时间最近”的个体之间的距离。我希望它计算所有及时“接近”的个体之间的距离。
在我的示例中,我有 3 只驼鹿和 3 只狼。我想拿驼鹿 1 并计算同时记录的 wolf 1 然后 wolf 2 然后 wolf 3 位置之间的距离。现在脚本只搜索任何狼之间的绝对最小时间差并计算那只狼的距离而不是所有其他人。
这是我的测试数据:
驼鹿位置数据:
structure(list(id = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L), .Label = c("F07001",
"F07010", "M07012"), class = "factor"), x = c(1482445L, 1481274L,
1481279L, 1481271L, 1480849L, 1480881L, 1480883L, 1480880L, 1482448L,
1482494L, 1482534L, 1482534L, 1482553L, 1482555L, 1482414L, 1482852L,
1476120L, 1476104L, 1476101L), y = c(6621768L, 6619628L, 6619630L,
6619700L, 6620321L, 6620427L, 6620438L, 6620423L, 6616403L, 6616408L,
6616395L, 6616408L, 6616406L, 6616418L, 6616755L, 6616312L, 6623655L,
6623646L, 6623652L), date = structure(c(1173088800, 1173096000,
1173103260, 1173110400, 1173117600, 1173211200, 1173218400, 1173139200,
1173088800, 1173096000, 1173103260, 1173110400, 1173117600, 1173211200,
1173218400, 1173139200, 1173270600, 1173277800, 1173282960), class = c("POSIXct",
"POSIXt"), tzone = "UTC")), .Names = c("id", "x", "y", "date"
), row.names = c(NA, -19L), class = "data.frame")
狼定位数据:
structure(list(id = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L), .Label = c("HF7572",
"Htest", "UM1347"), class = "factor"), x = c(1480610L, 1480640L,
1480613L, 1480613L, 1480555L, 1480567L, 1480627L, 1480532L, 1480593L,
1484394L, 1484394L, 1483940L, 1483933L, 1483935L, 1483930L, 1483855L,
1483793L, 1483802L, 1484392L, 1483855L), y = c(6619853L, 6619739L,
6619759L, 6619862L, 6619838L, 6619772L, 6619902L, 6619899L, 6619887L,
6619589L, 6619602L, 6619899L, 6619907L, 6619905L, 6619896L, 6619834L,
6619702L, 6619672L, 6619558L, 6619834L), date = structure(c(1173088800,
1173096060, 1173103440, 1173111600, 1173117780, 1173213600, 1173218400,
1173141120, 1173266100, 1173095940, 1173099600, 1173103200, 1173106920,
1173110400, 1173208800, 1173211200, 1173222000, 1173266100, 1173362100,
1173211200), class = c("POSIXct", "POSIXt"), tzone = "UTC")), .Names = c("id",
"x", "y", "date"), row.names = c(NA, -20L), class = "data.frame")
到目前为止,这是我的脚本:
mloc=read.csv("moose.csv", head = T)
wloc=read.csv("wolf.csv", head=T)
mloc$date<-as.POSIXct(strptime(mloc$date,"%Y-%m-%d %H:%M"),tz="UTC")
wloc$date<-as.POSIXct(strptime(wloc$date,"%Y-%m-%d %H:%M"),tz="UTC")
#sort the data sequentially by date time then convert to number
Sortmoose = mloc[order(mloc$date),]
Sortwolf = wloc[order(wloc$date),]
m <- as.numeric(Sortmoose$date)
w <- as.numeric(Sortwolf$date)
#Creates index of the time intervals
id <- findInterval(m, w, all.inside=TRUE)
id_min <- ifelse(abs(m-w[id])<abs(m-w[id+1]), id, id+1)
Sortmoose$wolfID = Sortwolf$id[id_min]
Sortmoose$wolfdate =Sortwolf$date[id_min]
Sortmoose$wolfx = Sortwolf$x[id_min]
Sortmoose$wolfy = Sortwolf$y[id_min]
Sortmoose$dist= sqrt((Sortmoose$wolfx-Sortmoose$x)^2+(Sortmoose$wolfy-Sortmoose$y)^2)
只要位置是在“同一”时间记录的,我想计算每对驼鹿/狼对之间的距离。我希望输出包含驼鹿信息和相关的狼信息以及这两点之间的距离(以米为单位)。我也想要时差,所以我可以过滤掉那些> 45分钟或类似的东西,但这是我认为我以后可以做的事情。基本上类似于:mooseID mooseDate mooseX mooseY wolfID wolfDate wolfX wolfY Distance(m) TimeDiff (min)