Conditionals in R – For, While, Repeat Loops

R Programming

 

Some example codes from R
course. Enhanced with comments and presented in a Knit HTML format.

IF ELSE in R

x=3 #initialize x as 3
y=100000 #initializde y to an arbitrary number
if(x>3){
  y<-100
} else {
  y=10
}
x # Print x
## [1] 3
y # see the new value of y after the if-else command
## [1] 10

Try writing the same code
in a different way:

x=3 #initialize x as 3
y=100000 #initializde y to an arbitrary number
y <- if(x > 3) {
 1000
} else { 
 10
}
x # Print x
## [1] 3
y # see the new value of y after the if-else command
## [1] 10

FOR LOOP in R

Example 1

for(i in 1:5){print(i)}
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5

Example2

x=c("R","C","JAVA","PYTHON") #create a vector with 4 character variables
for(loop in 1:4){print(x[loop])} #break the tradion of sticking to "i"
## [1] "R"
## [1] "C"
## [1] "JAVA"
## [1] "PYTHON"

Example
3 using seq_along

x=c("R","C","JAVA","PYTHON") #create a vector with 4 character variables
for(content in seq_along(x)){print(x[content])} #break the tradion of sticking to "i"
## [1] "R"
## [1] "C"
## [1] "JAVA"
## [1] "PYTHON"

Example 4

x=c("R","C","JAVA","PYTHON") #create a vector with 4 character variables
for(progLang in x) {print(x[progLang])} #break the tradion of sticking to "i"
## [1] NA
## [1] NA
## [1] NA
## [1] NA

NESTED LOOPS

print each element of the
matric

m=matrix(1:20, 4,5) #create a matrix 4 rows and 5 columns
m # print matrix
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    5    9   13   17
## [2,]    2    6   10   14   18
## [3,]    3    7   11   15   19
## [4,]    4    8   12   16   20
nrow(m)
## [1] 4
ncol(m)
## [1] 5
#print each element of the matric
for(rows in seq_len(nrow(m))) {
  for(columns in seq_len(ncol(m))){
    print(m[rows,columns])
    }
  }
## [1] 1
## [1] 5
## [1] 9
## [1] 13
## [1] 17
## [1] 2
## [1] 6
## [1] 10
## [1] 14
## [1] 18
## [1] 3
## [1] 7
## [1] 11
## [1] 15
## [1] 19
## [1] 4
## [1] 8
## [1] 12
## [1] 16
## [1] 20

WHILE LOOPS in R

Be careful with while
loops and don’t create an infite one!!!

counter=0 #initialize a counter variable
while(counter<15){print(counter)
                counter=counter+2}
## [1] 0
## [1] 2
## [1] 4
## [1] 6
## [1] 8
## [1] 10
## [1] 12
## [1] 14

More than one condition
testing with WHILE loop

z <- 5
t1="test1"
t2="test2"
z
## [1] 5
t1
## [1] "test1"
t2
## [1] "test2"
while(z >= -5 && z <= 10) {
 #Flip the coin one time
 coin <- rbinom(n=1, size=1, prob=0.5) 
 #n=number of observations, size=number of trials 
 if(coin == 1) { ## output of the rbinom by chance 1
 z <- z + 1
 cat(paste(coin, t1,z,"\n"))
 } else {
 z <- z - 1
 cat(paste(coin, t2,z,"\n"))
 } 
}
## 0 test2 4 
## 1 test1 5 
## 0 test2 4 
## 1 test1 5 
## 1 test1 6 
## 0 test2 5 
## 1 test1 6 
## 1 test1 7 
## 1 test1 8 
## 0 test2 7 
## 1 test1 8 
## 0 test2 7 
## 1 test1 8 
## 0 test2 7 
## 0 test2 6 
## 1 test1 7 
## 0 test2 6 
## 0 test2 5 
## 0 test2 4 
## 1 test1 5 
## 0 test2 4 
## 0 test2 3 
## 1 test1 4 
## 0 test2 3 
## 1 test1 4 
## 1 test1 5 
## 0 test2 4 
## 0 test2 3 
## 1 test1 4 
## 1 test1 5 
## 0 test2 4 
## 1 test1 5 
## 1 test1 6 
## 1 test1 7 
## 0 test2 6 
## 1 test1 7 
## 1 test1 8 
## 0 test2 7 
## 1 test1 8 
## 1 test1 9 
## 0 test2 8 
## 0 test2 7 
## 0 test2 6 
## 0 test2 5 
## 0 test2 4 
## 0 test2 3 
## 1 test1 4 
## 0 test2 3 
## 0 test2 2 
## 0 test2 1 
## 0 test2 0 
## 0 test2 -1 
## 0 test2 -2 
## 1 test1 -1 
## 0 test2 -2 
## 1 test1 -1 
## 1 test1 0 
## 1 test1 1 
## 1 test1 2 
## 1 test1 3 
## 0 test2 2 
## 0 test2 1 
## 0 test2 0 
## 1 test1 1 
## 0 test2 0 
## 0 test2 -1 
## 1 test1 0 
## 1 test1 1 
## 0 test2 0 
## 1 test1 1 
## 0 test2 0 
## 0 test2 -1 
## 0 test2 -2 
## 1 test1 -1 
## 1 test1 0 
## 0 test2 -1 
## 1 test1 0 
## 0 test2 -1 
## 1 test1 0 
## 1 test1 1 
## 1 test1 2 
## 1 test1 3 
## 1 test1 4 
## 1 test1 5 
## 0 test2 4 
## 1 test1 5 
## 1 test1 6 
## 1 test1 7 
## 1 test1 8 
## 0 test2 7 
## 1 test1 8 
## 1 test1 9 
## 1 test1 10 
## 0 test2 9 
## 0 test2 8 
## 0 test2 7 
## 0 test2 6 
## 1 test1 7 
## 0 test2 6 
## 1 test1 7 
## 0 test2 6 
## 0 test2 5 
## 1 test1 6 
## 1 test1 7 
## 0 test2 6 
## 1 test1 7 
## 0 test2 6 
## 0 test2 5 
## 1 test1 6 
## 1 test1 7 
## 1 test1 8 
## 0 test2 7 
## 0 test2 6 
## 0 test2 5 
## 0 test2 4 
## 0 test2 3 
## 1 test1 4 
## 0 test2 3 
## 0 test2 2 
## 0 test2 1 
## 1 test1 2 
## 1 test1 3 
## 1 test1 4 
## 1 test1 5 
## 0 test2 4 
## 1 test1 5 
## 0 test2 4 
## 0 test2 3 
## 1 test1 4 
## 1 test1 5 
## 0 test2 4 
## 0 test2 3 
## 0 test2 2 
## 1 test1 3 
## 1 test1 4 
## 1 test1 5 
## 1 test1 6 
## 1 test1 7 
## 0 test2 6 
## 0 test2 5 
## 1 test1 6 
## 1 test1 7 
## 1 test1 8 
## 1 test1 9 
## 1 test1 10 
## 0 test2 9 
## 1 test1 10 
## 1 test1 11

REPEAT / BREAK in R

could be used for iterative solution

x0 <- 1
tol <- 1e-5
repeat {
 x1 <- sqrt(100)
 if(sqrt(x1 - x0) < tol) {
 break
 } else {
 x0 <- x1
 } 
}
x0
## [1] 10
x1
## [1] 10

 

Leave a Comment

Your email address will not be published. Required fields are marked *