chris20
chris20

Reputation: 47

Breaking a for loop

I'm trying to line up some timings from two different instruments. I want to find the smallest time difference between the two time series to start lining x1 up with x2.

In the example below I can get the smallest time difference between x1[1] and x2. What I want is to find the min difference beween x1[i] and x2, then x1[i+1] and x2, but if the difference in x1[i+1] and x2 is not smaller than x1[i] and x2 then I want to break the for loop. Here x1[4] and x1[5] are both 3 seconds different from x2[5] and x2[6] so should break where i = 5 but it does not and loops through the whole of 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
  }
}

Upvotes: 0

Views: 60

Answers (1)

A. Webb
A. Webb

Reputation: 26446

The problem is that you are resetting your Mindf to its initial (large) value at each iteration of the loop,

for (i in 1:length(x1)){
  Mindf <- 200000000
  ...
}

so that the if (!MinRow < Mindf) conditional never applies. The setting of Mindf to MinRow that occurs in the else branch is just reset to the initial value back at the top of the loop.

You probably wanted

Mindf <- 200000000

for (i in 1:length(x1)) {
  ...
}

Here is an alternative approach to finding the minimum distance point

Convert to a time format in a vectorized manner

x1 <- strptime(x1,format = "%H:%M:%S")
x2 <- strptime(x2,format = "%H:%M:%S")

The minimum occurs at

arrayInd(which.min(abs(outer(x1,x2,"-"))),c(length(x1),length(x2)))

#     [,1] [,2]
#[1,]    4    5

the 4th entry of x1 and 5th entry of x2.

Upvotes: 1

Related Questions