1. If statements
Now that you understand relational and logical operators, you are ready to add logic to your code that allows R to make decisions based on certain conditions. This logic is built from
2. If statements
"if statements". An if statement is your way of telling R, if this condition is true, then run this code, otherwise the condition is false, so skip it and move on. This flow chart shows exactly that logical process. To create an if statement in R, you use the following structure. Type if, add a logical condition in parenthesis, and inside the braces you write the code, or body, that you want to run if that condition is true.
3. Default if…
Here's an example. Let the numbers between 0 and 1 represent the probability that a company will default. To generate this number, we will use runif to get a random number between 0 and 1. You don't know what this number is in advance, but you want to plan accordingly! Specifically, if the chance is above point-5, you want your program to notify you by printing "Likely to default!". An if statement is perfect for this kind of logic. Inside the parenthesis, you use one of the relational operators that you learned earlier to create the condition, and inside the braces you add a print statement. Running this, the print statement was executed. This must mean that default_chance was above point-5, and sure enough, it was point-948. So what happened here? R first checked if the default_chance was above point-5, and it was, so then it ran the code inside the braces, printing out the statement. If we run this code again with a new default chance, the print statement doesn't execute. This must mean that the new random number generated was less than point-5, and sure enough, its point-395.
4. If-else
Instead of doing nothing if the default_chance is less than point-5, you could add more logic using an if else statement so that your program runs a different chunk of code if the condition is false. That looks like this. You can read this as, if this condition is true, run the first chunk of code, else, run the other chunk. The flow chart shows the logic. First, a test condition is checked, and depending on the result either the body of the if statement or the body of the else statement is executed.
5. Default if…else…
Extending our example, let's simply set the default_chance to point-4. An else statement is added with its own print statement, and when the program is run again, it prints out "No problems here!", which is the appropriate response since default_chance is below point-5.
6. If-else if-else
Finally, you can add a second condition with the logic of if, else if, else. The code is similar to the last example, except after the first else, another if statement is created to check a second condition. If the first condition is true, the first code chunk is executed, if the first condition is false, but the second condition is true, the second chunk is executed. And if both conditions are false, the code in the else statement is executed. Keep in mind that while we only used 2 if statements here,
7. Default if...else if...else
you can create any number of combinations that you want. Back in the example, maybe you are more risk averse, and also want to be warned if the default_chance is between point-3 and point-5. Using the logical operator AND, and an else if you could create the following code. The body of the first if statement is not executed because the default chance is less than point-5, but the body of the new else if statement is executed, because the chance is between point-3 and point-5. Take a closer look at the condition there to see how it is built from the logical operator AND. Now, because R executed a part of the if statement, the last else is not even checked and the if statement is finished.
8. Let's practice!
That's alot of new code! Don't worry, the next few exercises will give you plenty of practice with this. You can do it!