Я пытаюсь выстроить некоторые тайминги из двух разных инструментов. Я хочу найти наименьшую разницу во времени между двумя временными рядами, чтобы начать выравнивание x1 с помощью x2.Нарушение цикла for
В приведенном ниже примере я могу получить наименьшую разницу во времени между x1 [1] и x2. Я хочу найти разницу min между 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
}
}
Спасибо, это была глупая ошибка. Спасибо за альтернативный подход, это намного более элегантно. – chris20