In this article we will show how to use if and if else statement in R. In data science it is an important component in a lot of functions when performing data manipulation and data modelling.
Theory
In data science and analytics, we often want to perform different set of operations on a variable depending on its value. For example, you are working on a product recommendation engine and you have different suggestions for different age groups. If a customer is younger than 18 years old, they get recommendation A; if a customer is between 18 and 30, they get recommendation B; and if a customer is over 30 years old,they get recommendation C. Conditioning based on values is always an important topic and it’s best to learn it with an applied example.
Application
Below are the steps we are going to take to make sure we do learn how to do use if else statement in R:
- How to write if statement in R
- How to write if else statement in R
- How to write nested if else statement in R
Part 1: How to write if statement in R
In this section we will explore a simple if statement. What it does is it performs a test on the condition and returns a Boolean value (True/False). In case of our example, the first part is to check if a customer’s age is less than 18. And the output of that should be that they get a recommendation which consists of product A.
Here is a code framework of the if statement in R:
if (condition) {
execute-this-code
}
Now let’s create an age variable and assign 14 to it.
age<-14
Now we are ready to create our first if statement in R:
if (age < 18) {print("Your recommendation is product A")}
Output:
[1] "Your recommendation is product A"
It does exactly what we expect it to do. Now let's try to run the same code after setting age variable to 24 and see what the output will be:
age<-24
if (age < 18) {print("Your recommendation is product A")}
Output:
As we see in this case, the output is empty. What we want to have is an output that says that your recommendation is product B if your age is 24 in this case.
This takes us to the next section of this article.
Part 2: How to write if else statement in R
In this section we will explore the if else statement. It works the same way as an if statement while allowing us to add another condition. In case of our example, the first part is to check if a customer's age is less than 18, if True, then recommend product A, if False, then recommend product B.
Here is a code framework of the if else statement in R:
if (condition) {
execute-this-code
} else {
execute-this-code
}
Now let's create an age variable and assign 24 to it.
age<-24
Now we are ready to create our if else statement in R:
if (age < 18) {
print("Your recommendation is product A")
} else {
print("Your recommendation is product B")
}
Output:
[1] "Your recommendation is product B"
Exactly what we expect. If you set age to 14 and rerun the above chunk of code, it will recommend product A. Therefore our if else statement is robust and works in both cases.
Recall that in the beginning of the article, the way we set up the business problem is that there is a third condition if the customer is over 30 years old, they should get product C as recommendation.
This takes us to the next section of this article.
Part 3: How to write nested if else statement in R
As you follow the pattern of this article, you probably recognize what we will do in this section. Yes, you are correct, we will add multiple conditions to our if else statement in R.
Let's state our business problem again:
- If customer's age less than 18, then recommend product A.
- If customers age is greater or equal to 18 but less than 30, then recommend product B.
- If customer's age is greater than 30, then recommend product C.
The logic for the nested if else statement will be the same as in the section above, except we will be adding one more condition.
Here is a code framework of the nested if else statement in R:
if (condition-1) {
execute-this-code
} else if (condition-2) {
execute-this-code
} else {
execute-this-code
}
Now let's create an age variable and assign 35 to it.
age<-35
Now we are ready to create our nested if else statement in R:
if (age < 18) {
print("Your recommendation is product A")
} else if (age >= 18 & age <30) {
print("Your recommendation is product B")
} else {print("Your recommendation is product C")}
Output:
[1] "Your recommendation is product C"
Exactly what we expect. If you set age to 14 and rerun the above chunk of code, it will recommend product A. Then if you set age to 24 and rerun the above chunk of code again, it will recommend product B. Therefore our if else statement is robust and works in both cases.
Creating correct if else statements in R is very useful when working with dataframes if you want to add or remove variables based on certain conditions (and in some way is related to filtering) to clean up the data. You can also find more related articles in our Data Manipulation section.