Difference between revisions of "R/C2/Conditional-Statements/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(5 intermediate revisions by 2 users not shown)
Line 19: Line 19:
  
 
|| In this tutorial, we will learn about,
 
|| In this tutorial, we will learn about,
* Conditional statements
+
* '''Conditional statements'''
* '''if, else, '''and '''else if '''statements
+
* '''if, else, '''and '''else if statements'''
  
 
|-
 
|-
Line 29: Line 29:
 
[https://spoken-tutorial.org/ https://spoken-tutorial.org/]
 
[https://spoken-tutorial.org/ https://spoken-tutorial.org/]
 
|| To understand this tutorial, you should know,  
 
|| To understand this tutorial, you should know,  
* Basic data structures  
+
* Basic '''data structures'''
* Indexing and slicing data frames  
+
* '''Indexing''' and '''slicing data frames'''
  
 
If not, please locate the relevant tutorials on '''R''' on this website.
 
If not, please locate the relevant tutorials on '''R''' on this website.
Line 38: Line 38:
 
System Specifications
 
System Specifications
 
|| This tutorial is recorded on
 
|| This tutorial is recorded on
* '''Ubuntu Linux '''OS version '''16.04'''
+
* '''Ubuntu Linux '''OS version 16.04
* '''R''' version '''3.4.4'''
+
* '''R''' version 3.4.4
* '''RStudio''' version '''1.1.463'''
+
* '''RStudio''' version 1.1.463
  
Install '''R''' version '''3.2.0''' or higher.  
+
Install '''R''' version 3.2.0 or higher.  
 
|-
 
|-
 
|| Show slide
 
|| Show slide
Line 48: Line 48:
 
Download Files
 
Download Files
 
|| For this tutorial, we will use
 
|| For this tutorial, we will use
* A '''data frame''' '''moviesData.csv'''
+
* A '''data frame moviesData.csv'''
 
* A '''script''' file '''conditionalStats.R'''.
 
* A '''script''' file '''conditionalStats.R'''.
  
Line 66: Line 66:
 
Conditional Statements  
 
Conditional Statements  
 
||  
 
||  
* Conditional statements are used to execute some logical conditions in the code.
+
* '''Conditional statements''' are used to execute some logical '''conditions''' in the code.
* '''if''', '''else '''and '''else if '''are the basic conditional statements.  
+
* '''if, else '''and '''else if '''are the basic '''conditional statements'''.  
  
 
|-
 
|-
Line 73: Line 73:
 
|| Let us switch to '''RStudio'''.
 
|| Let us switch to '''RStudio'''.
 
|-
 
|-
|| Highlight '''conditionalStats.R''' in the '''Files '''window''' '''of '''RStudio '''
+
|| Highlight '''conditionalStats.R''' in the '''Files '''window of '''RStudio '''
 
|| Open the '''script conditionalStats.R '''in''' RStudio'''.  
 
|| Open the '''script conditionalStats.R '''in''' RStudio'''.  
 
|-
 
|-
 
|| Highlight''' inScore''' in the '''Source''' window  
 
|| Highlight''' inScore''' in the '''Source''' window  
|| Here, we have declared a vector''' inScore'''.  
+
|| Here, we have declared a '''vector inScore'''.  
  
The elements of this vector represent the runs scored by '''India''' in three different one-day matches.  
+
The elements of this '''vector''' represent the runs scored by '''India''' in three different one-day matches.  
 
|-
 
|-
 
|| Highlight''' ausScore''' in the '''Source''' window  
 
|| Highlight''' ausScore''' in the '''Source''' window  
|| Similarly, we have declared another vector''' ausScore'''.  
+
|| Similarly, we have declared another '''vector ausScore'''.  
  
The elements of this vector represent the runs scored by '''Australia''' in three different one-day matches.  
+
The elements of this '''vector''' represent the runs scored by '''Australia''' in three different one-day matches.  
 
|-
 
|-
 
|| Highlight the '''Source''' button
 
|| Highlight the '''Source''' button
|| Run this '''script''' by clicking on the '''Source''' button.  
+
|| '''Run''' this '''script''' by clicking on the '''Source''' button.  
 
|-
 
|-
 
|| Highlight '''movies''' in the '''Source''' window  
 
|| Highlight '''movies''' in the '''Source''' window  
Line 108: Line 108:
  
 
'''}'''
 
'''}'''
|| In the '''Source''' window, type the following command.  
+
|| In the '''Source''' window, type the following '''command'''.  
  
 
|-
 
|-
 
|| Highlight '''if''' in the '''Source''' window  
 
|| Highlight '''if''' in the '''Source''' window  
|| It means that '''if''' the '''condition '''is '''TRUE '''then execute the '''expression''' inside the curly brackets.
+
|| It means that if the '''condition '''is '''TRUE '''then execute the '''expression''' inside the curly brackets.
 
|-
 
|-
 
|| Highlight '''Run''' button in the '''Source''' window  
 
|| Highlight '''Run''' button in the '''Source''' window  
|| Save the '''script''' and run the current line by pressing '''Ctrl+Enter''' keys simultaneously.  
+
|| Save the '''script''' and '''run''' the current line by pressing '''Ctrl+Enter''' keys simultaneously.  
 
|-
 
|-
 
||  
 
||  
Line 136: Line 136:
  
 
'''}'''
 
'''}'''
|| In the '''Source''' window, type the following command.  
+
|| In the '''Source''' window, type the following '''command'''.  
 
|-
 
|-
 
|| Highlight '''Run''' button in the '''Source''' window  
 
|| Highlight '''Run''' button in the '''Source''' window  
|| Save the '''script''' and run the current line.  
+
|| Save the '''script''' and '''run''' the current line.  
 
|-
 
|-
 
|| Highlight''' inScore''' and '''ausScore''' in the '''Source''' window.
 
|| Highlight''' inScore''' and '''ausScore''' in the '''Source''' window.
Line 148: Line 148:
 
But no message is displayed on the '''Console.'''
 
But no message is displayed on the '''Console.'''
 
|-
 
|-
|| Highlight the last '''if''' statement in the '''Source''' window  
+
|| Highlight the last '''if statement''' in the '''Source''' window  
|| Here the '''if''' statement will be executed only when '''India''' scores more runs than '''Australia''' in the second one day match.  
+
|| 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.  
+
Hence, we need to add an '''else statement''' with another expression.  
  
This expression should be executed when the '''if''' condition is not satisfied.  
+
This expression should be executed when the '''if condition''' is not satisfied.  
 
|-
 
|-
|| Highlight the last '''if''' statement in the '''Source''' window  
+
|| Highlight the last '''if statement''' in the '''Source''' window  
|| Click on the last line of the '''if '''statement.  
+
|| Click on the last line of the '''if statement'''.  
 
|-
 
|-
 
|| [RStudio]
 
|| [RStudio]
Line 165: Line 165:
  
 
'''}'''
 
'''}'''
|| Now, type the following command.  
+
|| Now, type the following '''command'''.  
  
Please note that the '''else''' statement begins on the same line where '''if''' statement ends.  
+
Please note that the '''else statement''' begins on the same line where '''if statement''' ends.  
 
|-
 
|-
 
|| Highlight '''Run''' button in the '''Source''' window  
 
|| Highlight '''Run''' button in the '''Source''' window  
|| Save the '''script''' and run the current line.  
+
|| Save the '''script''' and '''run''' the current line.  
 
|-
 
|-
 
||  
 
||  
Line 178: Line 178:
 
|| Now, we get a message,  '''Australia won the second ODI!'''
 
|| Now, we get a message,  '''Australia won the second ODI!'''
 
|-
 
|-
|| Highlight the '''if-else''' statement in '''Source''' window  
+
|| Highlight the '''if-else statement''' in '''Source''' window  
|| There is another efficient way to write this '''if-else''' statement.  
+
|| There is another efficient way to write this '''if-else statement'''.  
 
|-
 
|-
 
|| [RStudio]
 
|| [RStudio]
Line 188: Line 188:
  
 
'''"Australia won the second ODI!")'''
 
'''"Australia won the second ODI!")'''
|| In the '''Source''' window, type the following command.  
+
|| In the '''Source''' window, type the following '''command'''.  
 
|-
 
|-
 
|| Highlight '''ifelse''' in the '''Source''' window
 
|| Highlight '''ifelse''' in the '''Source''' window
|| Here, we are using '''ifelse''' for comparing the elements of two different vectors.  
+
|| Here, we are using '''ifelse''' for comparing the elements of two different '''vectors'''.  
 
|-
 
|-
 
|| Highlight '''ifelse''' in the '''Source''' window
 
|| Highlight '''ifelse''' in the '''Source''' window
  
 
Highlight '''inScore[2] > ausScore[2] '''in the '''Source''' window
 
Highlight '''inScore[2] > ausScore[2] '''in the '''Source''' window
|| Here the '''ifelse''' statement has three arguments.
+
|| Here the '''ifelse statement''' has three '''arguments'''.
  
 
* '''test''' - It is an object which can be forced to a logical mode.  
 
* '''test''' - It is an object which can be forced to a logical mode.  
Line 206: Line 206:
 
* '''yes''' - It returns values for true elements of the '''test.'''
 
* '''yes''' - It returns values for true elements of the '''test.'''
  
Here, the first statement represents the value of the true element.  
+
Here, the first '''statement''' represents the value of the true element.  
 
|-
 
|-
 
|| Highlight '''Australia won the second ODI! '''in the Source window  
 
|| Highlight '''Australia won the second ODI! '''in the Source window  
Line 212: Line 212:
 
* '''no''' - It returns values for false elements of the '''test'''  
 
* '''no''' - It returns values for false elements of the '''test'''  
  
The second statement represents the value of the false element.  
+
The second '''statement''' represents the value of the false element.  
 
|-
 
|-
 
|| Highlight '''Run''' button in the '''Source''' window  
 
|| Highlight '''Run''' button in the '''Source''' window  
|| Save the '''script''' and run the current line.  
+
|| Save the '''script''' and '''run''' the current line.  
 
|-
 
|-
 
|| Highlight the output in the '''Console''' window  
 
|| Highlight the output in the '''Console''' window  
 
|| We get the same message,  '''Australia won the second ODI!'''
 
|| We get the same message,  '''Australia won the second ODI!'''
  
In the  '''Source'''  window, scroll up to locate the two vectors.
+
In the  '''Source'''  window, scroll up to locate the two '''vectors'''.
 
|-
 
|-
 
| | Highlight''' inScore''' and '''ausScore''' in the '''Source''' window  
 
| | Highlight''' inScore''' and '''ausScore''' in the '''Source''' window  
 
| | In the third one day match, '''India''' and '''Australia''' have scored the same number of runs.  
 
| | In the third one day match, '''India''' and '''Australia''' have scored the same number of runs.  
  
Let us use an '''if-else''' logic to find out the winner in this case.  
+
Let us use an '''ifelse''' logic to find out the winner in this case.  
 
|-
 
|-
 
| | [RStudio]
 
| | [RStudio]
Line 238: Line 238:
  
 
'''}'''
 
'''}'''
| | In the '''Source''' window, click on the next line after the ifelse statement.  
+
| | In the '''Source''' window, click on the next line after the '''ifelse statement'''.  
  
Now type the following command.  
+
Now type the following '''command'''.  
 
|-
 
|-
 
|| Highlight '''Run''' button in the '''Source''' window  
 
|| Highlight '''Run''' button in the '''Source''' window  
|| Save the '''script''' and run the current line.  
+
|| Save the '''script''' and '''run''' the current line.  
 
|-
 
|-
 
|| Highlight the output in the '''Console''' window  
 
|| Highlight the output in the '''Console''' window  
 
|| We get the message,  '''Australia won the third ODI! ''' This is not correct.  
 
|| We get the message,  '''Australia won the third ODI! ''' This is not correct.  
 
|-
 
|-
|| Highlight the '''if-else''' statement in the Source window  
+
|| Highlight the '''if-else statement''' in the Source window  
 
|| So, we need to modify our logic.  
 
|| So, we need to modify our logic.  
 
|-
 
|-
Line 266: Line 266:
  
 
'''}'''
 
'''}'''
| | In the '''Source''' window, type the following command.  
+
| | In the '''Source''' window, type the following '''command'''.  
  
  
Please note that '''else''' '''if''' statement begins on the same line where '''if''' statement ends.  
+
Please note that '''else if statement''' begins on the same line where '''if statement''' ends.  
 
|-
 
|-
 
|| Highlight '''Run''' button in the '''Source''' window  
 
|| Highlight '''Run''' button in the '''Source''' window  
|| Run the current line.  
+
|| '''Run''' the current line.  
 
|-
 
|-
 
||  
 
||  
Line 280: Line 280:
 
|| Now, we got the correct message,  '''Third ODI was a tie. '''
 
|| Now, we got the correct message,  '''Third ODI was a tie. '''
 
|-
 
|-
|| Highlight the last if-else statement in the '''Source''' window  
+
||
|| A conditional structure contains, only one '''if '''statement.
+
|| I will resize the '''Console''' window again.
 +
|-
 +
|| Highlight the last '''if-else statement''' in the '''Source''' window  
 +
|| A '''conditional structure''' contains only one '''if statement'''.
  
It may contain as many '''else if '''statements''' '''as you need and only one '''else '''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'''.  
+
The '''else statement''' will be executed only when all the above '''if '''and '''else if statements''' are '''FALSE'''.  
 
|-
 
|-
 
|| Highlight '''ifelse''' statement in the Source window  
 
|| Highlight '''ifelse''' statement in the Source window  
 
|| In the '''Source''' window, scroll up.  
 
|| In the '''Source''' window, scroll up.  
  
Here, we have used '''ifelse''' for comparing the elements of two different vectors.  
+
Here, we have used '''ifelse''' for comparing the elements of two different '''vectors'''.  
 
|-
 
|-
 
|| Highlight '''ifelse''' in the '''Source''' window
 
|| Highlight '''ifelse''' in the '''Source''' window
|| Now, we will learn how to use '''ifelse''' statements for comparing the two columns of a '''data frame'''.  
+
|| Now, we will learn how to use '''ifelse statements''' for comparing the two columns of a '''data frame'''.  
 
|-
 
|-
 
|| Highlight '''movies''' in the '''Source''' window  
 
|| Highlight '''movies''' in the '''Source''' window  
|| In the '''Source''' window, scroll down to locate the last if-else statement.  
+
|| 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, click on '''movies data frame. '''
Line 303: Line 306:
 
|| In the '''Source''' window, scroll from left to right.  
 
|| In the '''Source''' window, scroll from left to right.  
  
This will enable us to see the remaining objects of '''movies''' '''data frame'''.  
+
This will enable us to see the remaining objects of '''movies data frame'''.  
 
|-
 
|-
 
|| Highlight '''audience_score''' and '''critics_score''' in '''movies'''  
 
|| Highlight '''audience_score''' and '''critics_score''' in '''movies'''  
|| Let us compare the '''critics_score '''and '''audience_score''' .  
+
|| 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'''
+
|| Show slide
* 0,''' '''otherwise'''. '''
+
 
 +
Example
 +
 
 +
||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.
  
 
|-
 
|-
Line 321: Line 329:
  
 
'''View(movies)'''
 
'''View(movies)'''
|| In the '''Source''' window, type the following command.  
+
|| In the '''Source''' window, type the following '''command'''.  
 
|-
 
|-
 
|| Highlight '''Run''' button in the '''Source''' window  
 
|| Highlight '''Run''' button in the '''Source''' window  
|| Save the '''script''' and run the last two lines.  
+
|| Save the '''script''' and '''run''' the last two lines.  
 
|-
 
|-
 
|| Highlight '''movies''' in the '''Source''' window  
 
|| Highlight '''movies''' in the '''Source''' window  
Line 336: Line 344:
  
 
Remember, 1 means '''audience_score''' is greater than '''critics_score'''.  
 
Remember, 1 means '''audience_score''' is greater than '''critics_score'''.  
 +
|-
 +
|| Show slide
  
Now, we will find the number of '''movies''', in which '''audience_score '''is greater than '''critics_score'''.  
+
Example
 +
 
 +
||
 +
* 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'''.
  
For this, we will use the '''sum''' function along with the '''if''' condition.
 
 
|-
 
|-
 
|| Highlight the '''script conditionalStats.R''' in the '''Source''' window  
 
|| Highlight the '''script conditionalStats.R''' in the '''Source''' window  
Line 347: Line 360:
  
 
'''sum(ifelse(movies$audience_score > movies$critics_score, 1, 0))'''
 
'''sum(ifelse(movies$audience_score > movies$critics_score, 1, 0))'''
|| In the '''Source''' window, type the following command.  
+
|| In the '''Source''' window, type the following '''command'''.  
 
|-
 
|-
 
|| Highlight '''Run''' button in the '''Source''' window  
 
|| Highlight '''Run''' button in the '''Source''' window  
|| Save the '''script''' and run the current line.  
+
|| Save the '''script''' and '''run''' the current line.  
 
|-
 
|-
 
|| Highlight the output in the Console window  
 
|| Highlight the output in the Console window  
Line 362: Line 375:
 
Summary
 
Summary
 
|| In this tutorial, we have learnt about,
 
|| In this tutorial, we have learnt about,
* Conditional statements
+
* '''Conditional statements'''
* '''if, else, '''and '''else if '''statements
+
* '''if, else, '''and '''else if statements'''
  
 
|-
 
|-
Line 370: Line 383:
 
Assignment
 
Assignment
 
|| We now suggest an assignment.
 
|| We now suggest an assignment.
* Use the built-in data set '''iris''' . Find the '''Species''', in which '''Sepal.Length''' is greater than '''Petal.Length'''
+
* Use the '''built-in data set iris'''. Find the species, in which '''Sepal.Length''' is greater than '''Petal.Length'''
* Count all such '''Species'''.  
+
* Count all such species.  
  
 
|-
 
|-
Line 413: Line 426:
  
 
Thank You
 
Thank You
|| The script for this tutorial was contributed by Varshit Dubey (CoE Pune).
+
|| 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.
 
This is Sudhakar Kumar from IIT Bombay signing off. Thanks for watching.
 
|-
 
|-
 
|}
 
|}

Latest revision as of 13:28, 23 September 2019

Title of the script: Conditional Statements

Author: Varshit Dubey (CoE Pune) and Sudhakar Kumar (IIT Bombay)

Keywords: R, RStudio, conditional, if, else, else if, video tutorial

Visual Cue Narration
Show slide

Opening Slide

Welcome to this tutorial on Conditional Statements.
Show slide

Learning Objective

In this tutorial, we will learn about,
  • Conditional statements
  • if, else, and else if statements
Show slide

Pre-requisites

https://spoken-tutorial.org/

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.

Show slide

System Specifications

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.

Show slide

Download Files

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.

[Computer screen]

Highlight moviesData.csv and conditionalStats.R in the folder conditionalStatements

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.

Show slide

Conditional Statements

  • 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.
Highlight conditionalStats.R in the Files window of RStudio Open the script conditionalStats.R in RStudio.
Highlight inScore in the Source window Here, we have declared a vector inScore.

The elements of this vector represent the runs scored by India in three different one-day matches.

Highlight ausScore in the Source window Similarly, we have declared another vector ausScore.

The elements of this vector represent the runs scored by Australia in three different one-day matches.

Highlight the Source button Run this script by clicking on the Source button.
Highlight movies in the Source window movies data frame opens in the Source window.

This data frame will be used later in this tutorial.

Highlight the script conditionalStats.R in the Source window Click on the script conditionalStats.R
Highlight inScore and ausScore in the Source window Let us now find which country has won the first one day match.
[RStudio]

if(inScore[1] > ausScore[1]){

print("India won the first ODI!")

}

In the Source window, type the following command.
Highlight if in the Source window It means that if the condition is TRUE then execute the expression inside the curly brackets.
Highlight Run button in the Source window Save the script and run the current line by pressing Ctrl+Enter keys simultaneously.
I will resize the Console window.
Highlight the output in 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.
Highlight inScore and ausScore in the Source window Let us now find which country won the second one day match.
I will resize the Console window.
[RStudio]

if (inScore[2] > ausScore[2]){

print("India won the second ODI!")

}

In the Source window, type the following command.
Highlight Run button in the Source window Save the script and run the current line.
Highlight inScore and ausScore in the Source window.

Highlight the output in the Console window

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.

Highlight the last if statement in the Source window 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.

Highlight the last if statement in the Source window Click on the last line of the if statement.
[RStudio]

else{

print("Australia won the second ODI!")

}

Now, type the following command.

Please note that the else statement begins on the same line where if statement ends.

Highlight Run button in the Source window Save the script and run the current line.
I will resize the Console window.
Highlight the output in the Console window Now, we get a message, Australia won the second ODI!
Highlight the if-else statement in Source window There is another efficient way to write this if-else statement.
[RStudio]

ifelse (inScore[2] > ausScore[2],

"India won the second ODI!",

"Australia won the second ODI!")

In the Source window, type the following command.
Highlight ifelse in the Source window Here, we are using ifelse for comparing the elements of two different vectors.
Highlight ifelse in the Source window

Highlight inScore[2] > ausScore[2] in the Source window

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.

Highlight India won the second ODI! in the Source window
  • yes - It returns values for true elements of the test.

Here, the first statement represents the value of the true element.

Highlight Australia won the second ODI! in the Source window
  • no - It returns values for false elements of the test

The second statement represents the value of the false element.

Highlight Run button in the Source window Save the script and run the current line.
Highlight the output in the Console window We get the same message, Australia won the second ODI!

In the Source window, scroll up to locate the two vectors.

Highlight inScore and ausScore in the Source window 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.

[RStudio]

if (inScore[3] > ausScore[3]){

print("India won the third ODI!")

} else{

print("Australia won the third ODI!")

}

In the Source window, click on the next line after the ifelse statement.

Now type the following command.

Highlight Run button in the Source window Save the script and run the current line.
Highlight the output in the Console window We get the message, Australia won the third ODI! This is not correct.
Highlight the if-else statement in the Source window So, we need to modify our logic.
[RStudio]

if (inScore[3] > ausScore[3]){

print("India won the third ODI!")

} else if (inScore[3] == ausScore[3]){

print("Third ODI was a tie.")

} else {

print("Australia won the third ODI!")

}

In the Source window, type the following command.


Please note that else if statement begins on the same line where if statement ends.

Highlight Run button in the Source window Run the current line.
I will resize the Console window.
Highlight the output in the Console window Now, we got the correct message, Third ODI was a tie.
I will resize the Console window again.
Highlight the last if-else statement in the Source window 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.

Highlight ifelse statement in the Source window In the Source window, scroll up.

Here, we have used ifelse for comparing the elements of two different vectors.

Highlight ifelse in the Source window Now, we will learn how to use ifelse statements for comparing the two columns of a data frame.
Highlight movies in the Source window In the Source window, scroll down to locate the last ifelse statement.

In the Source window, click on movies data frame.

Highlight the scroll bar in the Source window In the Source window, scroll from left to right.

This will enable us to see the remaining objects of movies data frame.

Highlight audience_score and critics_score in movies Let us compare the critics_score and audience_score.
Show slide

Example

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.
Highlight the script conditionalStats.R in the Source window Click on the script conditionalStats.R
[RStudio]

movies$dev <- ifelse(movies$audience_score > movies$critics_score, 1, 0)

View(movies)

In the Source window, type the following command.
Highlight Run button in the Source window Save the script and run the last two lines.
Highlight movies in the Source window movies data frame opens in the Source window.
Highlight the scroll bar in the Source window In the Source window, scroll from left to right.
Highlight dev in movies A new column named dev has been added. It has 1 or 0.

Remember, 1 means audience_score is greater than critics_score.

Show slide

Example

  • 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.
Highlight the script conditionalStats.R in the Source window Click on the script conditionalStats.R
[RStudio]

sum(ifelse(movies$audience_score > movies$critics_score, 1, 0))

In the Source window, type the following command.
Highlight Run button in the Source window Save the script and run the current line.
Highlight the output in the Console window So, there are 312 movies, in which audience_score is greater than critics_score.
Let us summarize what we have learnt.
Show slide

Summary

In this tutorial, we have learnt about,
  • Conditional statements
  • if, else, and else if statements
Show slide

Assignment

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.
Show slide

About the Spoken Tutorial Project

The video at the following link summarises the Spoken Tutorial project.

Please download and watch it.

Show slide

Spoken Tutorial Workshops

We conduct workshops using Spoken Tutorials and give certificates.

Please contact us.

Show Slide

Forum to answer questions

Please post your timed queries in this forum.
Show Slide

Forum to answer questions

Please post your general queries in this forum.
Show Slide

Textbook Companion

The FOSSEE team coordinates the TBC project.

For more details, please visit these sites.

Show Slide

Acknowledgment

The Spoken Tutorial project is funded by NMEICT, MHRD, Govt. of India
Show Slide

Thank You

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.

Contributors and Content Editors

Nancyvarkey, Sudhakarst