Welcome to this tutorial on Conditional Statements.
In this tutorial, we will learn about,
Conditional statements
if, else, and else if statements
To understand this tutorial, you should know,
Basic data structures
Indexing and slicing data frames
If not, please locate the relevant tutorials on R on this website.
This tutorial is recorded on
Ubuntu Linux OS version 16.04
R version 3.4.4
RStudio version 1.1.463
Install R version 3.2.0 or higher.
For this tutorial, we will use
A data frame moviesData.csv
A script file conditionalStats.R.
Please download these files from the Code files link of this tutorial.
I have downloaded and moved these files to conditionalStatements folder.
This folder is located in myProject folder on my Desktop.
I have also set conditionalStatements folder as my Working Directory.
Conditional statements are used to execute some logical conditions in the code.
if, else and else if are the basic conditional statements.
Let us switch to RStudio.
Open the script conditionalStats.R in RStudio.
Here, we have declared a vector inScore.
The elements of this vector represent the runs scored by India in three different one-day matches.
Similarly, we have declared another vector ausScore.
The elements of this vector represent the runs scored by Australia in three different one-day matches.
Run this script by clicking on the Source button.
movies data frame opens in the Source window.
This data frame will be used later in this tutorial.
Click on the script conditionalStats.R
Let us now find which country has won the first one day match.
In the Source window, type the following command.
It means that if the condition is TRUE then execute the expression inside the curly brackets.
Save the script and run the current line by pressing Ctrl+Enter keys simultaneously.
I will resize the Console window.
As India scored more runs than Australia in the first one day match, the message, India won the first ODI!, is displayed.
Let us now find which country won the second one day match.
I will resize the Console window.
In the Source window, type the following command.
Save the script and run the current line.
As Australia scored more runs than India in the second one day match, we should get a message, Australia won the second ODI!.
But no message is displayed on the Console.
Here the if statement will be executed only when India scores more runs than Australia in the second one day match.
Hence, we need to add an else statement with another expression.
This expression should be executed when the if condition is not satisfied.
Click on the last line of the if statement.
Now, type the following command.
Please note that the else statement begins on the same line where if statement ends.
Save the script and run the current line.
I will resize the Console window.
Now, we get a message, Australia won the second ODI!
There is another efficient way to write this if-else statement.
In the Source window, type the following command.
Here, we are using ifelse for comparing the elements of two different vectors.
Here the ifelse statement has three arguments.
test - It is an object which can be forced to a logical mode.
Here test is for the comparison of scores.
yes - It returns values for true elements of the test.
Here, the first statement represents the value of the true element.
no - It returns values for false elements of the test
The second statement represents the value of the false element.
Save the script and run the current line.
We get the same message, Australia won the second ODI!
In the Source window, scroll up to locate the two vectors.
In the third one day match, India and Australia have scored the same number of runs.
Let us use an ifelse logic to find out the winner in this case.
In the Source window, click on the next line after the ifelse statement.
Now type the following command.
Save the script and run the current line.
We get the message, Australia won the third ODI! This is not correct.
So, we need to modify our logic.
In the Source window, type the following command.
Please note that else if statement begins on the same line where if statement ends.
Run the current line.
I will resize the Console window.
Now, we got the correct message, Third ODI was a tie.
I will resize the Console window again.
A conditional structure contains only one if statement.
It may contain as many else if statements as you need
and only one else statement.
The else statement will be executed only when all the above if and else if statements are FALSE.
In the Source window, scroll up.
Here, we have used ifelse for comparing the elements of two different vectors.
Now, we will learn how to use ifelse statements for comparing the two columns of a data frame.
In the Source window, scroll down to locate the last ifelse statement.
In the Source window, click on movies data frame.
In the Source window, scroll from left to right.
This will enable us to see the remaining objects of movies data frame.
Let us compare the critics_score and audience_score.
We will add a new column named dev in the movies data frame, which will show 1, if audience_score is greater than critics_score and 0, otherwise.
Click on the script conditionalStats.R
In the Source window, type the following command.
Save the script and run the last two lines.
movies data frame opens in the Source window.
In the Source window, scroll from left to right.
A new column named dev has been added. It has 1 or 0.
Remember, 1 means audience_score is greater than critics_score.
Now, we will find the number of movies, in which audience_score is greater than critics_score.
For this, we will use the sum function along with the if condition.
Click on the script conditionalStats.R
In the Source window, type the following command.
Save the script and run the current line.
So, there are 312 movies, in which audience_score is greater than critics_score.
Let us summarize what we have learnt.
In this tutorial, we have learnt about, Conditional statements, if, else, and else if statements
We now suggest an assignment.
Use the built-in data set iris.
Find the species, in which Sepal.Length is greater than Petal.Length Count all such species.
The video at the following link summarises the Spoken Tutorial project.
Please download and watch it.
We conduct workshops using Spoken Tutorials and give certificates.
Please contact us.
Please post your timed queries in this forum.
Please post your general queries in this forum.
The FOSSEE team coordinates the TBC project.
For more details, please visit these sites.
The Spoken Tutorial project is funded by NMEICT, MHRD, Govt. of India
The script for this tutorial was contributed by Varshit Dubey (College of Engineering Pune).
This is Sudhakar Kumar from IIT Bombay signing off.
Thanks for watching.