title: “Class5_Loops” author: “Nick Vohra” date: “9/3/2019” output: html_document

Class 5 - Loops

In this Class, we looked at the very important coding principle of Loops

Loops take three main forms:

###### for ###### while ###### if…else ##### In this class we will focus on if…else and while loops

# Lets first focus on the if...else loop. 

# What an if...else loop is really saying is, 

# if somethign is true, do this

# if something is false, do this instead. 
# heres a simple example, lets say I ain't to run a code if x is positive and run a different code if x is negative. 

# TO do this, we need somehting called operators. 

# Step 1: Operators:
  
    # Logical Operators
    
# A > B     A is greater than B
# A >= B    A is greater than or equal to B
# A < B     A is less than B
# A <= B    A is less than or equal to B
# A == B    A is equal to B
# A != B    A is NOT equal to B

  # Mathematical Operators
  
# + Addition
# - Subtraction
# * Multiplication
# / Division
# ^ Exponent 
# %%    Modulus (Remainder from Division) 

# From our desire to tell if a number is positive or negative,
# we need the > or < sign to distinguish if a number is below 0 or not

# Step 2:
  # The syntax and the form of the if...else loop 

#           if(something is true){
  #         Do this chunk of code
  #          }else{
              # do this chunk of code  }
              
# Step 3: 
  # Test it out
  x = 5
  if(x>0){
  
  print("Positive")
  
    }else{
  
  print("Negative")}
## [1] "Positive"
# Thus, we have created a simple loop that tells us if x is positive or negative

The for Loop

The for loop operated through each element in a vector and applies code to it. You can also couple the for loop and the if/else loop to properly apply code to each element is it meets a certain criteria.

# Lets create a for loop that iterates through an entire vector and count up the even numbers in the vector. 

# We need to first create an arbitrary number vector:
  
  MyVector = c(5,15,78,95,12,74,32,10,5,42,65,78,94,51,12,20)
  
# How to count up the even numbers:
# 1) go through each element in the vector
# 2) think of a way to determine if a number is even or not
# 3) Count those up and deliveer the total number

# # Form of a for loop 
# for (item in vector){
# Do this code to the item
# }
# 

count = 0
for (item in MyVector){
if(item %% 2 == 0){
count = count + 1
}
}
count # equals 7 meaning that the total count of even numbers
## [1] 10
# We have now gone through each end of a vector  
# We checked if the number is even
# then added to the count variable.  

Position vectors in R

# using a for loop to sift though a data set and separate data based on treatment
Treatment = c(1,2,1,3,2,1,3,2,3,2)
Sex = c(1,1,0,1,0, 1, 0, 1, 0, 0)
Cell_Count = c(132, 123,548,421,848,951,951,357,854,212)
df = data.frame(Treatment, Sex, Cell_Count) 
df
##    Treatment Sex Cell_Count
## 1          1   1        132
## 2          2   1        123
## 3          1   0        548
## 4          3   1        421
## 5          2   0        848
## 6          1   1        951
## 7          3   0        951
## 8          2   1        357
## 9          3   0        854
## 10         2   0        212
# How to contruct a loop that we need to sift through the data and store it based on out citeria
CellCountTreat_1 = c()
CellCountTreat_2 = c()
CellCountTreat_3 = c()
# Whats the magic word? - INDEXING
for(item in 1:length(Treatment)){
  if(Treatment[item] == 1){
    CellCountTreat_1 = c(CellCountTreat_1,Cell_Count[item])
  } 
  if(Treatment[item] == 2){
    CellCountTreat_2 = c(CellCountTreat_2, Cell_Count[item])
  } 
  if(Treatment[item] == 3){
    CellCountTreat_3 = c(CellCountTreat_3, Cell_Count[item])
  }
}
CellCountTreat_1
## [1] 132 548 951
CellCountTreat_2
## [1] 123 848 357 212
CellCountTreat_3
## [1] 421 951 854
boxplot(CellCountTreat_1, CellCountTreat_2, CellCountTreat_3)
# Quick note on the use of the tilde in Boxplot
boxplot(Cell_Count ~ Treatment) # No need for loops