Loops in R

Three catagories:

While, for, if/else

The if loop

The if loop offers us a condition to pass a certain code.

So, if something is true, then do the following. It sort of acts as a gate keeper for code.

Heres what a basic if loop looks like, pay attention to the syntax

x = 5

if(x > 3){
  
print("X is greater than 3")

  }
## [1] "X is greater than 3"

Lets break that down, the first line initializes a variable. We cannot have a condition statement without something to test! then we tell the computer that if x is greater than three (if the condition is…(wait for it)….TRUE!), go ahead with the code, otherwise, just skip over it.

using else can allow one to give alternate code in their if statement. Check out the code below, but don’t forget to look at the syntax

x = 5

if(x>3){
  
  print("X is greater than 3")
} else{
  print("X is less than or equal to 3")
}
## [1] "X is greater than 3"

Notice that the else statement is on teh same line that terminates the if statement. These two have to be linked.

else if

One final form of the if loop allows multilpe conditions to be tested one after another

x = 3

if(x> 3){
  
  print("X is greater than 3")
} else if (x < 3){
  
  print(" X is less than 3")
}else{
  print( " X is 3 ")
  
}
## [1] " X is 3 "

Talk yourself through the above code. notice the loops are on the same line as teh previous ones termination } bracket.

For loop

The for loop cycles through each component in a vector, and applies a code to it, whatever that is.

The for loop is a powerful weapon is splitting up data, and even creating new data.

# Create a Vector to apply the for loop to
MyVector = c(1,2,2,1,2,1,2,1,2,1,2,1,1,2,2,2,2,1,2,1,1,2,1,2,1,2,2,1,1,2)
# Lets count how many 2's are in the vector

# Look at the below for loop, and how its structured
Number_of_2s = 0
for(value in MyVector){
 
   if (value == 2){
  
       Number_of_2s = Number_of_2s + 1
   }
  
}

Number_of_2s
## [1] 16

Lets break that down, we applied a for loop to every “value” in MyVector. For loops work like the following

for every element in this vector, apply this code. the “element” valiable can be names anything, I just chose “value” above to make it seem easier. It could’ve been named anything.

So now the code will cycle through each number in MyVector and apply the if loop to it. Notice how the brackets line up, the for loop doesn’t close until the end.

also notice that we had to intialize the count at zero so that the for loop had something to count up to.

You can even create new vectors as a result of the for loop.

lets say I want to split the following vectors into even and odd numbers.

Full_Vector = c(98,7,65,56,78,90,98,765,3,4,56,5,4,32,3,4,567,89,876,5,32,3,6,7,76,54,32,32,1,23,456,78,90,98,765,4,56,5,43,21)
Even_Num = c()
Odd_num = c()

for(Number in Full_Vector){
  
  if(Number %% 2 == 0){
    Even_Num = c(Even_Num, Number)
  } else{
    Odd_num = c(Odd_num, Number)
  }
}
Even_Num
##  [1]  98  56  78  90  98   4  56   4  32   4 876  32   6  76  54  32  32
## [18] 456  78  90  98   4  56
Odd_num
##  [1]   7  65 765   3   5   3 567  89   5   3   7   1  23 765   5  43  21

While Loop

The while loop is the one main type of loop we have not covered in class

The while loop allows one to run a code while a certain condition is true

The moment that condition ceases to be try, the loop terminates.

Look at the code below and walk through it

count = 0

while (count < 5 ){
  print(paste0("Have Not Reached 5 yet, the count is ", count)) # You 
  count = count + 1
}
## [1] "Have Not Reached 5 yet, the count is 0"
## [1] "Have Not Reached 5 yet, the count is 1"
## [1] "Have Not Reached 5 yet, the count is 2"
## [1] "Have Not Reached 5 yet, the count is 3"
## [1] "Have Not Reached 5 yet, the count is 4"

The while condition statement is volatile. Imagine if we make condition statement

count = 6
while (count > 5){
CODE CODE CODE
count = count + 1

}

This is an infinite loop and, under certain circumstances, can crash our program

Avoiding infitite loops is imperative, always check your loop before you run them.

count = 0
MyCondition = FALSE

while (MyCondition == FALSE){
  
  count = count + 1
  
  if (count == 15){
  
      MyCondition = TRUE
  
      }
  
  print(count)
}
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## [1] 6
## [1] 7
## [1] 8
## [1] 9
## [1] 10
## [1] 11
## [1] 12
## [1] 13
## [1] 14
## [1] 15

Using logical operators (TRUE/FALSE) to simple keep a loop active until the required conditions are met is a common use of the While loop

End of Introduction to Loops

The compllexity of a loop is up to you, you can nest all of these into one massive method of combing through data.