我试图从两种不同的乐器中排列一些时间。我想找到两个时间序列之间的最小时间差,以便用x2开始x1。
在下面的例子中,我可以得到x1 [1]和x2之间的最小时间差。我想要的是找到x1 [i]和x2之间的最小差异,然后是x1 [i + 1]和x2,但是如果x1 [i + 1]和x2的差异不小于x1 [i]和x2然后我想打破for循环。这里x1 [4]和x1 [5]都是3秒不同于x2 [5]和x2 [6]所以应该在i = 5的地方中断,但它不会在整个x1中循环。
x1 <- c("11:30:27", "11:30:37", "11:30:47", "11:30:57", "11:31:07", "11:31:17", "11:31:27", "11:31:37", "11:31:47",
"11:31:57", "11:32:07", "11:32:17", "11:32:27", "11:32:37", "11:32:47", "11:32:57", "11:33:07", "11:33:17",
"11:33:27", "11:33:37", "11:33:47", "11:33:57", "11:34:07", "11:34:17", "11:34:27", "11:34:37", "11:34:47",
"11:34:57", "11:35:07", "11:35:17", "11:35:27", "11:35:37", "11:35:47", "11:35:57", "11:36:07", "11:36:17",
"11:36:27", "11:36:37", "11:36:47", "11:36:57", "11:37:07", "11:37:17", "11:37:27", "11:37:37", "11:37:47",
"11:37:57", "11:38:07", "11:38:17", "11:38:27", "11:38:37", "11:38:47", "11:38:57", "11:39:07", "11:39:17",
"11:39:27", "11:39:37", "11:39:47", "11:39:57")
x2 <- c("10:59:23", "10:59:33", "10:59:43", "10:59:53", "11:30:54",
"11:31:04", "11:31:14", "11:31:24", "11:31:34", "11:31:44", "11:31:54", "11:32:04", "11:32:14", "11:32:24",
"11:32:34", "11:32:44", "11:32:54", "11:33:04", "11:33:14", "11:33:24", "11:33:34", "11:33:44", "11:33:54",
"11:34:04", "11:34:14", "11:34:24", "11:34:34", "11:34:44", "11:34:54", "11:35:04", "11:35:14", "11:35:24",
"11:35:34", "11:35:44", "11:35:54", "11:36:04", "11:36:14", "11:36:24", "11:36:34", "11:36:44", "11:36:54",
"11:37:04", "11:37:14", "11:37:24", "11:37:34", "11:37:44", "11:37:54", "11:38:04", "11:38:14", "11:38:24",
"11:38:34", "11:38:44", "11:38:54", "11:39:04", "11:39:14", "11:39:24", "11:39:34", "11:39:44", "11:39:54",
"11:40:04", "11:40:14", "11:40:24", "11:40:34", "11:40:44", "11:40:54", "11:41:04", "11:41:14", "11:41:24",
"11:41:34", "11:41:44", "11:41:54", "11:42:04", "11:42:14", "11:42:24", "11:42:34", "11:42:44", "11:42:54",
"11:43:04", "11:43:14", "11:43:24", "11:43:34", "11:43:44", "11:43:54", "11:44:04", "11:44:14", "11:44:24",
"11:44:34", "11:44:44", "11:44:54", "11:45:04", "11:45:14", "11:45:24", "11:45:34", "11:45:44", "11:45:54",
"11:46:04", "11:46:14", "11:46:24", "11:46:34", "11:46:44", "11:46:54", "11:47:04", "11:47:14", "11:47:24")
x2[which(abs(as.numeric(difftime(strptime(x1[1], format = "%H:%M:%S"), strptime(x2, format = "%H:%M:%S")))) ==
min(abs(as.numeric(difftime(strptime(x1[1], format = "%H:%M:%S"), strptime(x2, format = "%H:%M:%S"))))))]
for (i in 1:length(x1)){
Mindf <- 200000000
MinRow <- min(abs(as.numeric(difftime(strptime(x1[i], format = "%H:%M:%S"), strptime(x2, format = "%H:%M:%S")))))
if (!MinRow < Mindf){
j <- i
break
} else {
Mindf <- MinRow
}
}
答案 0 :(得分:1)
问题是您在循环的每次迭代中将Mindf
重置为其初始(大)值,
for (i in 1:length(x1)){
Mindf <- 200000000
...
}
以便if (!MinRow < Mindf)
条件从不适用。 Mindf
分支中MinRow
到else
的设置只会重置为循环顶部的初始值。
你可能想要
Mindf <- 200000000
for (i in 1:length(x1)) {
...
}
以下是寻找最小距离点的另一种方法
以矢量化方式转换为时间格式
x1 <- strptime(x1,format = "%H:%M:%S")
x2 <- strptime(x2,format = "%H:%M:%S")
最低限度发生在
arrayInd(which.min(abs(outer(x1,x2,"-"))),c(length(x1),length(x2)))
# [,1] [,2]
#[1,] 4 5
x1
的第4个条目和x2
的第5个条目。