Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Laboratory 02

Control flow

Now that we defined some fundemental objects and operations in R, we need to explore how R, and many other programmign language execute code. R executes code sequentially from top to bottom. The line of R code at the top is run first, then the second highest line, then the third highest and so on.

When code is executed line by line from top to bottom it is called sequential control. Sequentuntial control if the default way R executes statements.

However, we can change the order in which R executes lines of code in three ways: (i) choice (ii) repetition (iii) functions (we’ll talk much more about functions next week).

Choice or Selection

An R program is a set of statements meant to produce one or more results. We can choose to execute all the lines in our program, or we can choose to execute some lines and not others depedent on conditions. When we execute some lines of R code to run and not others we are using a specific type of control flow called Selection (“we select some lines and not others”).

While we often define statements as sentences that are either TRUE or FALSE, in R we define a logical statement that evaluates to TRUE or to FALSE an expression.

The main ways to control which lines to execute is with the if/else, if/elsif/else, and the switch statement.

If/else

The following study at this link = Rajkumar et al. (2006) chose to study the effects of a novel treatment on newly diagnosed myleoma. The study investigators random;y assigned 103 patientrs to receive a control therapy and 104 patients to receieve the novel treatment. The primary outcome of interest was the rate of response among patients in control and treament where a response was defined as a 50% or greater decrease in detection of the cancer.

We could decide to define a vector vv that will contain three pieces of information: a patient id that is an integer used to link a single patient to their clincial records, whether the patient was assigned to recieve a treatment or control therapy, and the percent response where 100 means complete response/reduction and 0 means no response/reduction.

``` {}
\PY{n}{pt} \PY{o}{=} \PY{n+nf}{c}\PY{p}{(}\PY{l+m}{1256}\PY{p}{,} \PY{l+s}{\PYZdq{}}\PY{l+s}{TREATMENT\PYZdq{}}\PY{p}{,} \PY{l+m}{87}\PY{p}{)}
```

We stored a vector called pt with our three pieces of info. Now suppose we want to add an additional piece of info to our patient vector that determines if the patient had a succussful or unsuccussful response.

We need the if/else sytax. The if/else is an expression placed inside of parentheses and two blocks of code. if the expression evaluates to TRUE the first block of code will be executed and the second block is skipped. If the expression evaluates to FALSE then the second block will be executed and first block will be skippedd.

``` {}
\PY{n+nf}{if}\PY{p}{(} \PY{n}{pt}\PY{p}{[}\PY{l+m}{3}\PY{p}{]} \PY{o}{\PYZgt{}} \PY{l+m}{50} \PY{p}{)}\PY{p}{\PYZob{}}  \PY{c+c1}{\PYZsh{} Our expression is pt[3] \PYZgt{} 50}
    \PY{n}{pt} \PY{o}{=} \PY{n+nf}{c}\PY{p}{(}\PY{n}{pt}\PY{p}{,}\PY{l+m}{1}\PY{p}{)}
\PY{p}{\PYZcb{}}\PY{n}{else}\PY{p}{\PYZob{}}
    \PY{n}{pt} \PY{o}{=} \PY{n+nf}{c}\PY{p}{(}\PY{n}{pt}\PY{p}{,}\PY{l+m}{0}\PY{p}{)}
\PY{p}{\PYZcb{}}
```

We expect that the code above will create a new vector c(1256,"TREATMENT",87,1). To be sure, lets check the variable pt by printing it.

``` {}
\PY{n+nf}{print}\PY{p}{(}\PY{n}{pt}\PY{p}{)}
```
[1] "1256"      "TREATMENT" "87"        "1"

if/elseif/else and switch

The if/else handles one condition that either evaluates to TRUE or FALSE. However, we may need to execute a specific piece of code that depends on more than one condition. The if/else if/else syntax can execute a specific block of code dependent on more than one condition.

For example, suppose that we want to classify a patient’s response as either above 75, between 50 and 75, not including 50 and 75, or less than or equal to 50. Let us also assume our patient is a following vector:

``` {}
\PY{n}{pt} \PY{o}{=} \PY{n+nf}{c}\PY{p}{(}\PY{l+m}{6587}\PY{p}{,}\PY{l+s}{\PYZdq{}}\PY{l+s}{CONTROL\PYZdq{}}\PY{p}{,}\PY{l+m}{24}\PY{p}{)}
```

We can use the if /else if/ else syntax.

``` {}
\PY{n+nf}{if}\PY{p}{(} \PY{n}{pt}\PY{p}{[}\PY{l+m}{3}\PY{p}{]} \PY{o}{\PYZgt{}} \PY{l+m}{75} \PY{p}{)}\PY{p}{\PYZob{}}  
    \PY{n}{pt} \PY{o}{=} \PY{n+nf}{c}\PY{p}{(}\PY{n}{pt}\PY{p}{,}\PY{l+m}{2}\PY{p}{)}
\PY{p}{\PYZcb{}}\PY{n}{else} \PY{n+nf}{if }\PY{p}{(} \PY{n}{pt}\PY{p}{[}\PY{l+m}{3}\PY{p}{]} \PY{o}{\PYZgt{}}\PY{l+m}{50} \PY{p}{)} \PY{p}{\PYZob{}}
    \PY{n}{pt} \PY{o}{=} \PY{n+nf}{c}\PY{p}{(}\PY{n}{pt}\PY{p}{,}\PY{l+m}{1}\PY{p}{)}
\PY{p}{\PYZcb{}}\PY{n}{else} \PY{p}{\PYZob{}}\PY{n}{pt} \PY{o}{=} \PY{n+nf}{c}\PY{p}{(}\PY{n}{pt}\PY{p}{,}\PY{l+m}{0}\PY{p}{)} \PY{p}{\PYZcb{}} \PY{c+c1}{\PYZsh{} Our if/else if/else will run this code bc the above two conditions are not met.}
```

The if/else if/ else syntax begins at the top condition (pt``[``3``]`` > 75). If the top condition is FALSE then the condition second to the top is evaluated next ( pt``[``3``]`` > 50). If the second condition is FALSE, and there are no additional else if conditions, then code in the else block is executed.

``` {}
\PY{n+nf}{print}\PY{p}{(}\PY{n}{pt}\PY{p}{)}
```
[1] "6587"    "CONTROL" "24"      "0"

Compund expressions

The expressions we used above evaluated a single expression as either TRUE or FALSE. At times we may need more complicated expressions that must evaluate several expressions at once. We will call these compund expressions.

For example, we may want to treatment response values different if the patient was assigned to treatment versus control. Suppose if a patient is assigned control and has a response above 50 they receieve a 1, if a patient is assigned control with a response equal to or less than 50 they recieve a 0. If a patient is assigned treatment and has a response above 50 they receieve a 2 and if their response is equal to or less than 50 they recieve a 3.

We need to evaluate two expressions based on the patient assignment and response. We are allowed to include logical comparisons (LAB01) inside expressions.

``` {}
\PY{n}{pt} \PY{o}{=} \PY{n+nf}{c}\PY{p}{(}\PY{l+m}{1359}\PY{p}{,} \PY{l+s}{\PYZdq{}}\PY{l+s}{TREATMENT\PYZdq{}}\PY{p}{,} \PY{l+m}{72}\PY{p}{)}

\PY{n+nf}{if}\PY{p}{(} \PY{p}{(}\PY{n}{pt}\PY{p}{[}\PY{l+m}{3}\PY{p}{]} \PY{o}{\PYZgt{}} \PY{l+m}{50}\PY{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}{(}\PY{n}{pt}\PY{p}{[}\PY{l+m}{3}\PY{p}{]} \PY{o}{\PYZlt{}=} \PY{l+m}{50}\PY{p}{)} \PY{o}{\PYZam{}\PYZam{}} \PY{p}{(}\PY{n}{pt}\PY{p}{[}\PY{l+m}{2}\PY{p}{]}\PY{o}{==}\PY{l+s}{\PYZdq{}}\PY{l+s}{TREATMENT\PYZdq{}}\PY{p}{)} \PY{p}{)}\PY{p}{\PYZob{}}
    \PY{n}{pt} \PY{o}{=} \PY{n+nf}{c}\PY{p}{(}\PY{n}{pt}\PY{p}{,}\PY{l+m}{3}\PY{p}{)}
\PY{p}{\PYZcb{}}

\PY{c+c1}{\PYZsh{} check our work}
\PY{n+nf}{print}\PY{p}{(}\PY{n}{pt}\PY{p}{)}
```
[1] "1359"      "TREATMENT" "72"        "2"

The above code used the logical comparison AND (&&) to test whether the patient was treatment/control and if there repsonse was above or below 50.

Repitition

Often we will need to repeat a certain number of lines of code. We may need to apply the same operation to many different patients or observations or apply some set of code repeatedly until a condition it met. Loops allows us to repeat lines of code before continuing to execute lines of code below our loop.

While loop

The while loop repeats lines of code---called a block---until a specific condition is met.

``` {}
\PY{n}{x} \PY{o}{=} \PY{l+m}{2}
\PY{n+nf}{while}\PY{p}{(}\PY{n}{x}\PY{o}{\PYZlt{}}\PY{l+m}{1000}\PY{p}{)}\PY{p}{\PYZob{}}
    \PY{n}{x} \PY{o}{=} \PY{n}{x}\PY{o}{\PYZca{}}\PY{p}{(}\PY{l+m}{2}\PY{p}{)}
\PY{p}{\PYZcb{}}
\PY{n+nf}{print}\PY{p}{(}\PY{n}{x}\PY{p}{)}
```
[1] 65536

In the code above, we used the syntax while (CONDITION) {CODE} to execute a while loop. The code above assigned the variable x the value 2. The next line of code was the while loop. Because the condition evaluated to FALSE (x was not less than 1000) the code inside the parentheses---the code block---was executed once. After executing the code block the condition x<1000 was tested again. Becasue the condition evaluated to FALSE, the code block was executed again, and again, and again, until the condition evaluated to TRUE. After the code block evaluated to TRUE, we continued to execute lines below the while loop in sequence.

The above code is equivalent to the following expanded code

``` {}
\PY{n}{x}\PY{o}{=}\PY{l+m}{2}
\PY{n+nf}{if }\PY{p}{(}\PY{n}{x} \PY{o}{\PYZlt{}}\PY{l+m}{1000}\PY{p}{)}\PY{p}{\PYZob{}}
    \PY{n}{x}\PY{o}{=}\PY{n}{x}\PY{o}{\PYZca{}}\PY{l+m}{2}
\PY{p}{\PYZcb{}}
\PY{n+nf}{if }\PY{p}{(}\PY{n}{x} \PY{o}{\PYZlt{}}\PY{l+m}{1000}\PY{p}{)}\PY{p}{\PYZob{}}
    \PY{n}{x}\PY{o}{=}\PY{n}{x}\PY{o}{\PYZca{}}\PY{l+m}{2}
\PY{p}{\PYZcb{}}
\PY{n+nf}{if }\PY{p}{(}\PY{n}{x} \PY{o}{\PYZlt{}}\PY{l+m}{1000}\PY{p}{)}\PY{p}{\PYZob{}}
    \PY{n}{x}\PY{o}{=}\PY{n}{x}\PY{o}{\PYZca{}}\PY{l+m}{2}
\PY{p}{\PYZcb{}}
\PY{n+nf}{if }\PY{p}{(}\PY{n}{x} \PY{o}{\PYZlt{}}\PY{l+m}{1000}\PY{p}{)}\PY{p}{\PYZob{}}
    \PY{n}{x}\PY{o}{=}\PY{n}{x}\PY{o}{\PYZca{}}\PY{l+m}{2}
\PY{p}{\PYZcb{}}
\PY{n+nf}{if }\PY{p}{(}\PY{n}{x} \PY{o}{\PYZlt{}}\PY{l+m}{1000}\PY{p}{)}\PY{p}{\PYZob{}}
    \PY{n}{x}\PY{o}{=}\PY{n}{x}\PY{o}{\PYZca{}}\PY{l+m}{2}
\PY{p}{\PYZcb{}}
\PY{n+nf}{if }\PY{p}{(}\PY{n}{x} \PY{o}{\PYZlt{}}\PY{l+m}{1000}\PY{p}{)}\PY{p}{\PYZob{}}
    \PY{n}{x}\PY{o}{=}\PY{n}{x}\PY{o}{\PYZca{}}\PY{l+m}{2}
\PY{p}{\PYZcb{}}
\PY{n+nf}{if }\PY{p}{(}\PY{n}{x} \PY{o}{\PYZlt{}}\PY{l+m}{1000}\PY{p}{)}\PY{p}{\PYZob{}}
    \PY{n}{x}\PY{o}{=}\PY{n}{x}\PY{o}{\PYZca{}}\PY{l+m}{2}
\PY{p}{\PYZcb{}}
\PY{n+nf}{if }\PY{p}{(}\PY{n}{x} \PY{o}{\PYZlt{}}\PY{l+m}{1000}\PY{p}{)}\PY{p}{\PYZob{}}
    \PY{n}{x}\PY{o}{=}\PY{n}{x}\PY{o}{\PYZca{}}\PY{l+m}{2}
\PY{p}{\PYZcb{}}
\PY{n+nf}{print}\PY{p}{(}\PY{n}{x}\PY{p}{)}
```
[1] 65536

and so the while loop is a natural way to repeat a series of R code until meeting some condition.

For loop

Suppose we want to execute a code a block a fixed number of times. We could use a while loop to square the variable x four times using the below code:

``` {}
\PY{n}{number\PYZus{}of\PYZus{}times}\PY{o}{=}\PY{l+m}{1}
\PY{n}{x} \PY{o}{=} \PY{l+m}{2}

\PY{n+nf}{while }\PY{p}{(}\PY{n}{number\PYZus{}of\PYZus{}times} \PY{o}{\PYZlt{}}\PY{l+m}{5}\PY{p}{)}\PY{p}{\PYZob{}}
    \PY{n}{x}\PY{o}{=}\PY{n}{x}\PY{o}{\PYZca{}}\PY{l+m}{2}
    \PY{n}{number\PYZus{}of\PYZus{}times} \PY{o}{=} \PY{n}{number\PYZus{}of\PYZus{}times}\PY{l+m}{+1}
\PY{p}{\PYZcb{}}
\PY{n+nf}{print}\PY{p}{(}\PY{n}{x}\PY{p}{)}
```
[1] 65536

Because repeating code a fixed number of times is needed so often to solve a problem, a second type of loop was created---the for loop. The for loop repeats a code block a fixed number of times by iterating through a sequence.

Sequences

A sequence is a mapping, or function, from the numbers 1,2,3,4,... to some set of items a,b,c,d,⋯a,b,c,d,\cdots. We often denote the items a,b,c,d,...a,b,c,d,... with a single variable name and a subscript that is called an index: a1,a2,a3,a4,⋯a_{1},a_{2},a_{3},a_{4},\cdots. In the above sequence the number 1 maps to a1a_{1}, the number 2 maps to a2a_{2} and so on.

We can create sequences of integers in R with the function seq. We can provide seq two integers, a “from” and “to” and this function will produce all integers between “from” and “to”, including both the “from” value and the “to” value. We will produce all integers from -2 to 8. Watch

``` {}
\PY{n+nf}{seq}\PY{p}{(}\PY{l+m}{\PYZhy{}2}\PY{p}{,}\PY{l+m}{8}\PY{p}{)}
```
-2

-1

0

1

2

3

4

5

6

7

8

This sort of request is so common in R that there is an easier way to write “from,to” sequences. We place the “from” value to the left and the “to” value to the right of a colon.

``` {}
\PY{l+m}{\PYZhy{}2}\PY{o}{:}\PY{l+m}{8}
```
-2

-1

0

1

2

3

4

5

6

7

8

R understand that the colon asks to produce all integers starting with the value on the left and ending with the value on the right.

Back to the for loop

Lets look how we can use sequences and the idea of the for loop to rewrite our code.

``` {}
\PY{n}{x}\PY{o}{=}\PY{l+m}{2}
\PY{n+nf}{for}\PY{p}{(}\PY{n}{number\PYZus{}of\PYZus{}times} \PY{n}{in} \PY{l+m}{1}\PY{o}{:}\PY{l+m}{4}\PY{p}{)}\PY{p}{\PYZob{}}
    \PY{n}{x}\PY{o}{=}\PY{n}{x}\PY{o}{\PYZca{}}\PY{l+m}{2}
\PY{p}{\PYZcb{}}
\PY{n+nf}{print}\PY{p}{(}\PY{n}{x}\PY{p}{)}
```
[1] 65536

We can think of the for loop above in expanded form.

``` {}
\PY{n}{x}\PY{o}{=}\PY{l+m}{2}

\PY{n}{x}\PY{o}{=}\PY{n}{x}\PY{o}{\PYZca{}}\PY{l+m}{2}
\PY{n}{x}\PY{o}{=}\PY{n}{x}\PY{o}{\PYZca{}}\PY{l+m}{2}
\PY{n}{x}\PY{o}{=}\PY{n}{x}\PY{o}{\PYZca{}}\PY{l+m}{2}
\PY{n}{x}\PY{o}{=}\PY{n}{x}\PY{o}{\PYZca{}}\PY{l+m}{2}

\PY{n+nf}{print}\PY{p}{(}\PY{n}{x}\PY{p}{)}
```
[1] 65536

Assignment

Lets use our new control flow skills to compute probabilities and conditional probabilities.

Run the below code to create one vector that describes whether patients were assigned to treatment or control and a second vector describing whether the patient had a succussful or unsuccesful status.

``` {}
\PY{n}{draw\PYZus{}random\PYZus{}number} \PY{o}{=} \PY{n+nf}{function}\PY{p}{(}\PY{p}{)}\PY{p}{\PYZob{}}
    \PY{n+nf}{return}\PY{p}{(}\PY{n+nf}{runif}\PY{p}{(}\PY{l+m}{1}\PY{p}{)}\PY{p}{)}
\PY{p}{\PYZcb{}}
```
Practice loop and conditional structure

After running the above code block, we can create a random number by running the following code draw_random_number()

  1. Run the code draw_random_number()

  2. Create and if/else statement that prints “Less than 1/2” when draw_random_number() is smaller than the value 0.5 and else prints “greater than or equal to 1/2” when draw_random_number() is 0.5 or larger.

  3. Create a variable category and assign this variable the value -1.

  4. Create an if/else if/else statement that assigns the variable category the value 1 when draw_random_number() is smaller than 0.25, the value 2 when draw_random_number() is greater than or equal to 0.25 and smaller than 0.50, the value 3 when draw_random_number() is greater than or equal to 0.50 and smaller than 0.75 and the value 4 otherwise.

  5. Create a vector random_values that is empty (i.e. random_values = c()).

  6. Assign the variable value equal to draw_random_number().

  7. Create a while loop with the condition that value returns a value less than 0.5 - Inside the while loop, in the code block, assign the variable value to a new random number draw_random_number() - Inside the while loop, in the code block, append value to your vector random_values

  8. Create a for loop that iterates the code block you create for the while loop 10 times.

  9. What can you say about the vector random_values that would be produced from 7. versus 8.?

``` {}
\PY{n}{patient\PYZus{}status}    \PY{o}{=} \PY{n+nf}{c}\PY{p}{(}\PY{l+m}{0}\PY{p}{,}\PY{l+m}{1}\PY{p}{,}\PY{l+m}{1}\PY{p}{,}\PY{l+m}{0}\PY{p}{,}\PY{l+m}{0}\PY{p}{,}\PY{l+m}{1}\PY{p}{,}\PY{l+m}{1}\PY{p}{,}\PY{l+m}{1}\PY{p}{,}\PY{l+m}{0}\PY{p}{,}\PY{l+m}{0}\PY{p}{,}\PY{l+m}{0}\PY{p}{,}\PY{l+m}{1}\PY{p}{,}\PY{l+m}{1}\PY{p}{,}\PY{l+m}{1}\PY{p}{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p}{,}\PY{l+s}{\PYZdq{}}\PY{l+s}{c\PYZdq{}}\PY{p}{,}\PY{l+s}{\PYZdq{}}\PY{l+s}{t\PYZdq{}}\PY{p}{,}\PY{l+s}{\PYZdq{}}\PY{l+s}{t\PYZdq{}}\PY{p}{,}\PY{l+s}{\PYZdq{}}\PY{l+s}{c\PYZdq{}}\PY{p}{,}\PY{l+s}{\PYZdq{}}\PY{l+s}{t\PYZdq{}}\PY{p}{,} \PY{l+s}{\PYZdq{}}\PY{l+s}{c\PYZdq{}}\PY{p}{,}\PY{l+s}{\PYZdq{}}\PY{l+s}{c\PYZdq{}}\PY{p}{,}\PY{l+s}{\PYZdq{}}\PY{l+s}{t\PYZdq{}}\PY{p}{,}\PY{l+s}{\PYZdq{}}\PY{l+s}{c\PYZdq{}}\PY{p}{,}\PY{l+s}{\PYZdq{}}\PY{l+s}{c\PYZdq{}}\PY{p}{)}
```
Lets create a for loop that will iterate through the positions of our two vectors patient_status and patient_treatment, performing different operations as we move to each item in the vector
  1. Use the length function to compute the number of patients in the study and store that length in the variable N

  2. Create a variable called num_of_treat_assigns and assign that variable the value 0

  3. Create a variable called num_of_contr_assigns and assign that variable the value 0

  4. Create a variable called num_of_status_success and assign that variable the value 0

  5. Create a for loop that will run a code block starting at 1 and ending at N

  6. Inside the code block of the for loop, use the variable num_of_status_success to count the number of patients with a succussful status

  7. Use N and num_of_status_success to compute the probability of a success

  8. Lets make our for loop more complicated. Inside our code block, create an if/else such that when patient treatment is a “t” we increment the variable num_of_treat_assigns by 1, else we increment the variable num_of_contr_assigns by 1.

  9. Lets add to our if/else statement. Create two variables num_of_trt_status_success and num_of_ctr_status_success and assign them both the value 0. Inside our loop, when a patient treatment is “t” we will implement step 8 and in addition we will increment the variable num_of_trt_status_success by 1. If the patient treatment is “c” then we will increment num_of_ctr_status_success by 1.

  10. Using num_of_trt_status_success and num_of_treat_assigns compute the conditional probability of success given the patient was assigned treatment.

  11. Using num_of_ctr_status_success and num_of_contr_assigns compute the conditional probability of success given the patient was assigned control.

References
  1. Rajkumar, S. V., Blood, E., Vesole, D., Fonseca, R., & Greipp, P. R. (2006). Phase III Clinical Trial of Thalidomide Plus Dexamethasone Compared With Dexamethasone Alone in Newly Diagnosed Multiple Myeloma: A Clinical Trial Coordinated by the Eastern Cooperative Oncology Group. Journal of Clinical Oncology, 24(3), 431–436. 10.1200/jco.2005.03.0221