## 3.1 Programming Control Flow

• Control flow (or flow control) is a well defined sequence of conditional statements, loops and statements which directs the R script (or code in generalised sense) to execute one thing or the other based on the conditions written in the program.

### 3.1.1 if-else Conditional Statements

• We use if-else conditional statements when we want the R program to branch out in different directions based on a logical condition.

• The following example compares the mean of two stocks and assigns a variable with the greater mean.

data_stocks = read.csv("data/us_stocks.csv")
# remove NAs from the data

data_stocks = na.omit(data_stocks)
m_msft = mean(data_stocks$MSFT) m_aapl = mean(data_stocks$AAPL)
if (m_msft > m_aapl) {
g_mean = m_msft
message("Msft mean is higher")
} else {
g_mean = m_aapl
message("Aapl mean is higher")
}
g_mean  #print greater mean
[1] 207.7967
• The if-else also works as a function call, the if-else call in the example above can be reduced to one line as follows.

• Note that the curly brackets in case of just one statement are optional. They are required in case of a block operation. Its easy to just use them to avoid confusion.

• R also has a function $$\mathtt{ifelse}$$ which does the same operation as in example above. See $$\mathtt{help(ifelse)}$$ for more details

# arguments to ifelse
args(ifelse)
function (test, yes, no)
NULL
g_mean = ifelse(m_msft > m_aapl, m_msft, m_aapl)
g_mean
[1] 207.7967

### 3.1.2 Loops

• Loops are the common feature in almost all the programming languages.

• R provides three basic loops using $$\mathtt{for}$$, $$\mathtt{while}$$ and $$\mathtt{repeat}$$.

# construct the loop
j = 0
for (i in 1:15) {
j = j + i  #add i to j
print(j)  #print the sequential sum
}
[1] 1
[1] 3
[1] 6
[1] 10
[1] 15
[1] 21
[1] 28
[1] 36
[1] 45
[1] 55
[1] 66
[1] 78
[1] 91
[1] 105
[1] 120

### 3.1.3$$\mathtt{while}$$ loop

• $$\mathtt{while}$$ loop evaluates an expression or a function while a condition is TRUE. Lets repeat the above example using $$\mathtt{while}$$ loop
# intialise j and i
j = 0
i = 1
# one can also use i<=15
while (i < 16) {
j = j + i
i = i + 1
print(j)
}
[1] 1
[1] 3
[1] 6
[1] 10
[1] 15
[1] 21
[1] 28
[1] 36
[1] 45
[1] 55
[1] 66
[1] 78
[1] 91
[1] 105
[1] 120

### 3.1.4$$\mathtt{repeat}$$ loop

• $$\mathtt{repeat}$$ repeats the same expression till it is broken due to a condition.
# intialize
j = 0
i = 1
repeat {
j = j + i
i = i + 1
print(j)
if (i > 15)
break
}
[1] 1
[1] 3
[1] 6
[1] 10
[1] 15
[1] 21
[1] 28
[1] 36
[1] 45
[1] 55
[1] 66
[1] 78
[1] 91
[1] 105
[1] 120

Together all these three loops can be used for iterative operations. The loops come handy when you have to iterate medium to large data by row or column.6

1. Using simple loops can be resource intensive for large datasets or operations. There are other approaches such as using iterative functions like $$\mathtt{lapply, sapply}$$ etc or parallel computing methods to get better results in such cases.↩︎