Toni Toni - 3 months ago 14
R Question

R loop with two break command

I am trying to determine the first decimal place value different from zero. For example, in

0.0000082109314
it would be the sixth (or million-th), where there is an
8
.

I thought about a loop, but it's not working. So I'd rather ask in pseudo-code:

d = runif(100, min = 1e-10, max = 1e-5) # Toy data with 100 elements in a vector
position = rep(0, 100) # Starting an empty vector to place results
for(j in 1:100){ # Looping through d
for(i in 1:10){ # Exponents from 1 to 10
if(d[j]]) * 10^i >= 1) # First power of 10 turning the value > or = 1
position[j] = i # Assign i to the position
stop the looping through i and move on to the next j
}
}


So I need the loop to stop replacing the
i
value as soon as the condition is fulfilled. Otherwise, any higher value of
i
will also meet the condition, and it won't return the desired first position different from zero.

I know about
break
and
next
, but how could I use them (or other commands) here?




The issue is to at some point within the loop (when the condition is met) to ask R to 1. Save the index, and 2. Move on to the next
j
.

position = rep(0, 100)
for(j in 1:100){
for(i in 1:10){
if(d[j]]) * 10^i >= 0) position[j] = i AND next
else
CONTINUE with i
}
}

Answer

break will kick you out of your current loop. It won't go all the way to the top level so if you use it inside of the loop indexed by i it will basically just kick you to the next value for j and restart i at 1.

Just as a note if you're going to have multiple conditions inside of your if statement make sure to wrap all of them in curly braces.

for(j in 1:3){
  for(i in 1L:6L){

    # The result of this if statement is that we skip this iteration
    # when i==2.
    if(i == 2){
      next
    }

    # The result of this if statement is that we kick out of the
    # for loop indexed by i.  The result being that we reach the end
    # of the code block for the for loop indexed by j so if we aren't
    # finished iterating over all of the values for j we go to the next
    # value for j and start the for loop with i all over again.
    if(i == 5){
      break
    }

    # Just print out what i and j are equal to. We do this after the
    # if statements so any iteration that isn't stopped by the if
    # statements will end up printing a result.
    print(sprintf("i: %i j: %i", i, j))
  }
}

gives the output

[1] "i: 1 j: 1"
[1] "i: 3 j: 1"
[1] "i: 4 j: 1"
[1] "i: 1 j: 2"
[1] "i: 3 j: 2"
[1] "i: 4 j: 2"
[1] "i: 1 j: 3"
[1] "i: 3 j: 3"
[1] "i: 4 j: 3"

so by using next I skip every iteration where i==2 and by using break I stop anything for i>=5 and move on to the next value for j

If you were having troubles getting break to work with your code you'd need to post what you actually tried. There were issues other than 'break' in your code (you use d[j]] notice the mismatched square braces and I think you messed up your parenthesis on your if statement). This is what I think you wanted:

d = runif(100, min = 1e-10, max = 1e-5) # Toy data with 100 elements in a vector
position = rep(0, 100)                  # Starting an empty vector to place results
for(j in 1:100){                        # Looping through d
  for(i in 1:10){                       # Exponents from 1 to 10
    if((d[j] * 10^i) >= 1){             # First power of 10 turning the value >  or = 1
        position[j] = i                     # Assign i to the position
        break
    }
  }
}