Adding flexibility to the conversation
Table of Contents
Introduction
Now we know a bit about the different data types and data structures that computers can understand. The next step is to learn about programming concepts.
Conditionals
With each data structure in the previous section, you've seen a piece of code used to create an example of that data structure. You know that we can create data types and store them with names as variables. But what if we wanted to query them. What if we only wanted to run our code if certain conditions were met? Such as I only want to analyse the data for participants that have no missing values, or only for people over the age of 75, or only for people that have brown hair? This is where conditionals come into the picture.
Have you ever played the game 'Simon Says'? Briefly, how the game works is that one person gives instructions. The rest of the people in the room are meant to follow the instructions if they were preceded by the words 'Simon says' and ignore the instructions otherwise. Anyone who performs an instruction that was not preceded by the words 'Simon says' is out of the game.
The game 'Simon Says' is a form of conditional. The person giving the instructions is acting as the programmer and the rest of the people in the room are acting as the computer. The condition to be met is that the programmer says the words 'Simon says'. This can be evaluated to TRUE
(in the case where the programmer did say 'Simon says') or FALSE
(in the case where the programmer didn't say 'Simon says'). The instructions for the computer to follow are different depending on whether the conditional was TRUE
(follow the instruction) or FALSE
(ignore the instruction).
Here is an example of a conditional written in R to check whether I should eat the tim tam. If I’m hungry (TRUE
) then the computer will print out "GIVE ME A TIM TAM!!" If I’m not hungry (FALSE
), the computer will tell me to politely decline the tim tam.
hungry <- TRUE
if (hungry){
print("GIVE ME A TIM TAM!!!")
} else {
print("No tim tam for me, thanks :)")
}
[1] "GIVE ME A TIM TAM!!!"
hungry <- FALSE
if (hungry){
print("GIVE ME A TIM TAM!!!")
} else {
print("No tim tam for me, thanks :)")
}
[1] "No tim tam for me, thanks :)"
Loops
One way that we can write code is to spell out each instruction one by one. However, sometimes this gets a bit repetitive... If we wanted to print the numbers 1:10, we could say print 1, print 2, print 3, print 4, etc. Or if we want to analyse patient data, we could say analyse data for patient 1, analyse data for patient 2, analyse data for patient 3, etc. If you have several hundred patients, that would end up being a lot of copy and paste! This is where loops come in handy.
When you’re repeating the same instructions - such as print or analyse - over different data, you can use a for loop. The same instructions are repeated but the data used is different for each iteration of the loop. An example of this would be asking for a person's name. The question ('What's your name?') will be the same every time, but the result will be different depending on the person that you ask! (Unless they are all called Barry, in which case you should consider broadening your friendship group).
Here is an example of the two step dance - universally popular with uncoordinated people - written in R. The steps have been repeated 4 times. Notice how much shorter the written code is compared to the output.
for (beats in 1:4){
print("bring left foot to right foot")
print("step left foot back to original position")
print("bring right foot to left foot")
print("step right foot back to original position")
}
[1] "bring left foot to right foot"
[1] "step left foot back to original position"
[1] "bring right foot to left foot"
[1] "step right foot back to original position"
[1] "bring left foot to right foot"
[1] "step left foot back to original position"
[1] "bring right foot to left foot"
[1] "step right foot back to original position"
[1] "bring left foot to right foot"
[1] "step left foot back to original position"
[1] "bring right foot to left foot"
[1] "step right foot back to original position"
[1] "bring left foot to right foot"
[1] "step left foot back to original position"
[1] "bring right foot to left foot"
[1] "step right foot back to original position"
Functions
The last concept covered in this section is functions. Functions are a series of instructions that have been given a name (yes, that means that they are variables!). Whenever we want to run that set of instructions we can call the function by name. This is again useful to reduce the amount of copy and paste required when repeating instructions.
An example might be that you are constantly wanting to convert units from Celsius to Fahrenheit. You could store the formula as a function called convertCelsiusToFahrenheit
and then call it each time you want to make that conversion. By storing the formula as a function, you don't need to remember the details of the formula anymore; you just need to remember the function name.
We use functions all the time! When you walk across the room, you are giving your brain the command to walk. But you don't consciously tell it which muscles need to be activated for that to happen. In this case the function would be named walk and the series of instructions required to walk would be stored in that function.
Another function can be found in cooking. When following a recipe, cookbooks often assume knowledge. They assume that you will understand that dice means take a knife and cut ingredients into small cubes. In this case the function name is dice and the instructions are to take a knife and cut ingredients into small cubes.
Here is an example of a function called moodCalendar
written in R. I have give you this function so that you can check what my mood is likely to be on any given day. You will then be able to make an informed decision as to whether you want to talk to me that day... It looks like Friday is a better option than Monday!
moodCalendar <- function(day){
if (day %in% c("Friday", "Saturday", "Sunday")){
print(paste(day, "is a happy day"))
} else if (day %in% c("Monday", "Tuesday")){
print(paste(day, "is a grumpy day"))
} else {
print(paste(day, "is a neutral day"))
}}
moodCalendar("Monday")
[1] "Monday is a grumpy day"
moodCalendar("Friday")
[1] "Friday is a happy day"