Conditions and Loops
Stand-alone statements
if statement
• An if statement runs a block of code only if a certain condition is true
• These constructs allow a program to respond differently depending
on whether a condition is TRUE or FALSE
• Syntax:
if(condition){
do any code here
}
else statement
• The if statement executes a chunk of code if and only if a defined
condition is true
• if you want something different to happen when the condition is false,
you can add an else declaration
• Syntax:
if(condition) {
do any code in here if condition is TRUE
}else{
do any code in here if condition is FALSE
}
Nested and Stacking statement
• if statement in another if statement is called as nested statement
• Syntax
if(condition){
if(condtion){
do any code here if both conditions are true
}else{
do any code here if inner if statement is true
}
do any code here if outer condition is true and inner condition is false
}else{
do any code if condition is false
}
switch Statement
• You need to choose which code to run based on the value of a single
object
• One option is to use a series of if statements, where you compare the
object with various possibe values to produce a logical value for each
condtion
• Syntax:
switch(EXPR,case 1,case 2....)
Coding Loops
for loop
• The for loop repeats the code as it works its way through a vector,
element by element
• Syntax:
for(loopindex in loopvector){
do any code here
}
• Here loopindex is a placeholder that represents an element in the
loopvector-it starts off as the first element in the vector and moves to
the next element with each loop repetition
Nested for loop
• for loop inside another for loop
• When a for loop is nested in another for loop, the inner loop is
executed in full before the outer loop loopindex is incremented, at
which point the inner loop is executed all over again
while loop
• The while loop repeats code until a specific condition evaluates to
FALSE
• Syntax
while(loopcondition){
do any code here
}
repeat and break
• It is a repeating a set of operations is the repeat statement
• there is no termination for the repeat statement. To terminate the
code it is mandatory to use break statement inside the if condition
• If there is no break statement the code will be in infinite loop
• Syntax
repeat{
do any code here
}
Fibonacci series
• To see repeat in action, let’s calculate the famous mathematical series
the fibonacci sequence.
• The fibonacci sequence is an infinte series of integers beginning with
1,1,2,3,5,8,13,.....
• Formula: Fn+1=Fn+Fn-1;
• where F1=F2=1
Implicit looping with apply
• In some situations, especially for relatively routine for loops (such as executing
some function on each member of a list), you can avoid some of the details
associated with explicit looping
eg:
foo<-matrix(1:12,4,3)
row.totals<-rep(NA,times=nrow(foo))
for(i in 1:nrow(foo)){
row.totals[i]<-sum(foo[i,])
}
row.totals
[1] 15 18 21 24
apply
• In the above example each row calculates and stores the sum in
row.totals.
• But, you can use apply to get the same result
Syntax:
apply(values,margin,function)
• where: values are the vector, margin is to know whether applying for
rows or columns, function: built in functions
• eg: apply(foo,margin=1,sum)
tapply
• The tapply function performs operations on subsets of the object of
interest, where those subsets are defined in terms of one or more
factor vectors
Syntax
tapply(vector,index,function)
eg: x<-c(2,1,3,5,6,1,2,4)
y<-c(“a”,”b”,”a”,”c”,”b”,”c”,”a”,”b”)
tapply(x,index=y,fun=sum)
lapply
• This helps to apply functions to the list
• Syntax:
lapply(vector,function)
Eg:
baz<-list(aa=c(1,2,3),bb=”string”,cc=matrix(1:4,2,2),dd=matrix(c(T,F,T,T),2,2))
lapply(baz,FUN=is.matrix)
It displays output in list format
sapply
• The sapply returns the same results as lapply but in array form
• Syntax
sapply(vector,function)
Eg:
baz<-list(aa=c(1,2,3),bb=”string”,cc=matrix(1:4,2,2),dd=matrix(c(T,F,T,T),2,2))
sapply(baz,FUN=is.matrix)
o/p
aa bb cc dd
FALSE FALSE TRUE TRUE