Controls and Loops
Overview
Teaching: 60 min
Exercises: 30 minQuestions
Popular controls and loops
Objectives
Introduce if and if-else statements
Introduce loops
Rmarkdown
Today, we are going to use another nifty feature of RStudio which makes working with R in a reproducible and effient manner a lot easier and more effective. Instead of saving our work as an Rscript, we will save it as an Rnotebook file, which will capture all of the code, output AND figures into one R document.
To do this go to File -> New Rnotebook and save that file.
R Markdown is a file format for making dynamic documents with R. An R Markdown document is written in markdown (an easy-to-write plain text format) and contains chunks of embedded R code, like the document below.
Controls & Loops
These allow you to control the flow of execution of a script. Common ones include:
- if, else
- for
If statements
if (condition_is_satisfied)
{
# do something
}
Hadley Wickham has published a style guide for R.
“An opening curly brace should never go on its own line and should always be followed by a new line. A closing curly brace should always go on its own line, unless it’s followed by else. Always indent the code inside curly braces.”
if (condition_is_satisfied) {
# do something
}
For example
mhi5 <- 80
if (mhi5 >= 60) {
print("Patient has good mental health")
}
[1] "Patient has good mental health"
Sometimes, we want to do something even if the if
statement returns FALSE
. In such a case, we use an else
statement
If-else statements
if (condition_is_satisfied) {
# do something
} else {
# do something else
}
For example, imagine we had a patient complete the SF-36 questionnaire. We have calculated that the patient has a Mental Health Inventory (5-item) score of 45.
mhi5 <- 45
if (mhi5 >= 60) {
print("Patient has good mental health")
} else {
print("Patient has poor mental health")
}
[1] "Patient has poor mental health"
If, else if and else statements
if (condition1_is_satisfied) {
# do something
} else if (condition2_is_satisfied) {
# do something else
} else {
# do something different
}
For example:
mhi5 <- 45
if (mhi5 >= 60) {
print("Patient has great mental health")
} else if (mhi5 <= 20) {
print ("Patient has poor mental health")
} else {
print("Patient has good mental health")
}
[1] "Patient has good mental health"
Section quiz
- Imagine a patient has just completed the SF-36 questionnaire and we have calculated that this patient has a MHI5 score of 45. Write a set of if-else statements that will test the variable ‘mhi5’. Print out the characters “Patient has poor mental health” if it is less than 52 and “Patient has good mental health” otherwise.
Solution
- If-else statements
mhi5 <- 45 if (mhi5 < 52) { print("Patient has poor mental health") } else { print("Patient has good mental health") }
For loops
For loops works on an iterable variable and assigns successive values till the end of a sequence.
This is useful if you have to perform a task over and over again.
My favourite analogy is imagine you check your email in the morning and you have 100 new items. You iteratively go through each one and perform some task on it e.g. reply, flag, report junk
for (item in list_of_items) {
# do something with the item
}
In a simple example of a for-loop, let’s just print out each item
myitems <- 1:10
for (i in myitems) {
print(i)
}
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10
We can use this format. This will iterative through the disorders
vector and each item will be assigned to a variable called d
. We can then do something with d
, such as printing it out.
disorders <- c("ocd", "autism", "depression", "ocd")
for (d in disorders) {
print(d)
}
[1] "ocd"
[1] "autism"
[1] "depression"
[1] "ocd"
Or can manipulate the d
variable
for (d in disorders) {
output <- paste("Diagnosis is", d, sep = " ")
print(output)
}
[1] "Diagnosis is ocd"
[1] "Diagnosis is autism"
[1] "Diagnosis is depression"
[1] "Diagnosis is ocd"
For loop examples
We’ve seen that we can use for
loops for vectors. We can also use for
loops for data frames.
First let’s read in the autism screening dataset from last lesson with the same modifications
autism.data <- read.csv(file = "data/autism_pids.csv",
header = TRUE,
sep = ",")
We can traverse across each row of the data frame using a for
loop
# for each row, print the participant's name and country
numrows <- nrow(autism.data)
for (i in 1:numrows) {
patientid <- autism.data[i , "pids"]
location <- autism.data[i , "country"]
output <- paste(patientid, "lives in", location, sep =" ")
print(output)
}
For-loops and if statements
We can incorporate our knowledge of if
statements with for
loops.
Following our email analogy, imagine you check your email in the morning and you have 10 new items. You iteratively go through each one and perform some task on it e.g. reply, flag, report junk.
We can put an if
statement such that if the email title is “You won’t believe how he lost 10kg in just 2 weeks”, then you will ‘report junk’.
Or if the email is from your boss, you will ‘flag’ it as important.
Here, we will go thorugh every row in the autism.data data frame and print out “PatientID_X diagnosed with ASD” for those with ASD according to this mobile app.
numrows <- nrow(autism.data)
for (i in 1:numrows) {
patientid <- autism.data[i , "pids"]
autism <- autism.data[i , "Class.ASD"]
if (autism == "YES") {
output <- paste(patientid, "diagnosed with ASD", sep =" ")
print(output)
}
}
Section quiz
- For each row in the autism.data data frame, test whether the participant has “YES” in the Class.ASD column. If so, then print out “Patient diagnosed with ASD” and if not, print out “No ASD”
Solution
- We will use a for-loop and a set of if-else statements within the loop.
numrows <- nrow(autism.data) for (i in 1:numrows) { if (autism.data[i, "Class.ASD"] == "YES") { print("Patient diagnosed with ASD") } else { print("No ASD") } }
Key Points
R markdown notebooks can be very powerful for keeping your code and output in one place
Use if and if-else statements to control the execution of your code
If you have to do something multiple times, you might consider using a loop