Matheus Rabetti
Matheus Rabetti

Reputation: 43

Repeat a for loop

I'm just testing the loop in a small dataset. In this case, the loop will be repeated only one time until my condition (both title and url having same lenght) mets.

linhas_title <- c(5, 19, 48, 90, 135, 179, 424, 479, 532)
linhas_url <- c(14, 43, 85, 130, 175, 474, 527, 566)

for(i in 1:(length(linhas_title)-1)){
  print(paste("Titulo:",i+1, "e", "Url: ", i))

  if(linhas_title[i+1] - linhas_url[i] < 0) {
    print(paste("Titulo", i, "excluido"))
    linhas_title <- linhas_title[-i]
    break 
  } else print(paste("Titulos e url pareados!"))
}

I did not want to break the loop, but restart it. I tried while and repeat functions but without success.

Upvotes: 1

Views: 496

Answers (1)

Se&#241;or O
Se&#241;or O

Reputation: 17412

If I'm reading correctly that you want to restart the entire loop when that if statement leads to break, I think you'll need to create a "wrapper":

linhas_title <- c(5, 19, 48, 90, 135, 179, 424, 479, 532)
linhas_url <- c(14, 43, 85, 130, 175, 474, 527, 566)

runAgain = TRUE

while(runAgain)
{
  runAgain = FALSE

  for(i in 1:(length(linhas_title)-1))
  {
    print(paste("Titulo:",i+1, "e", "Url: ", i))

    if(linhas_title[i+1] - linhas_url[i] < 0) 
    {
      print(paste("Titulo", i, "excluido"))
      linhas_title <- linhas_title[-i]
      runAgain = TRUE
      break 
    } 
    else 
      print(paste("Titulos e url pareados!"))
  }

}

Upvotes: 1

Related Questions