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

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with "'''Title of the script''': Conditional Statements '''Author''': Varshit Dubey (CoE Pune) and Sudhakar Kumar (IIT Bombay) '''Keywords''': R, RStudio, conditional, if, else,...")
 
Line 8: Line 8:
  
 
{| border =1
 
{| border =1
|'''Visual Cue’’’
+
|'''Visual Cue'''
|'''Narration’’’
+
|'''Narration'''
 
|-
 
|-
 
|| Show slide  
 
|| Show slide  
Line 19: Line 19:
  
 
Learning Objective
 
Learning Objective
 
 
  
 
|| 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 36: Line 33:
 
* 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 48: Line 43:
 
* '''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.  
Line 59: Line 52:
 
* A '''data frame''' '''moviesData.csv'''
 
* A '''data frame''' '''moviesData.csv'''
 
* A '''script''' file '''conditionalStats.R'''.
 
* A '''script''' file '''conditionalStats.R'''.
 
 
  
 
Please download these files from the '''Code files''' link of this tutorial.  
 
Please download these files from the '''Code files''' link of this tutorial.  
Line 68: Line 59:
 
Highlight '''moviesData.csv '''and''' conditionalStats.R '''in the folder '''conditionalStatements'''
 
Highlight '''moviesData.csv '''and''' conditionalStats.R '''in the folder '''conditionalStatements'''
 
|| I have downloaded and moved these files to '''conditionalStatements''' folder.  
 
|| I have downloaded and moved these files to '''conditionalStatements''' folder.  
 
  
 
This folder is located in '''myProject''' folder on my '''Desktop'''.
 
This folder is located in '''myProject''' folder on my '''Desktop'''.
 
  
 
I have also set '''conditionalStatements''' folder as my '''Working Directory.'''  
 
I have also set '''conditionalStatements''' folder as my '''Working Directory.'''  
Line 81: Line 70:
 
* 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 92: Line 80:
 
|| 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.  
Line 98: Line 85:
 
|| 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.  
Line 107: Line 93:
 
|| Highlight '''movies''' in the '''Source''' window  
 
|| Highlight '''movies''' in the '''Source''' window  
 
|| '''movies data frame''' opens in the '''Source''' window.  
 
|| '''movies data frame''' opens in the '''Source''' window.  
 
  
 
This '''data frame''' will be used later in this tutorial.  
 
This '''data frame''' will be used later in this tutorial.  
Line 116: Line 101:
 
|| Highlight''' inScore''' and '''ausScore''' in the '''Source''' window  
 
|| Highlight''' inScore''' and '''ausScore''' in the '''Source''' window  
 
|| Let us now find which country has won the first one day match.  
 
|| Let us now find which country has won the first one day match.  
 
 
  
 
|-
 
|-
Line 128: Line 111:
 
'''}'''
 
'''}'''
 
|| In the '''Source''' window, type the following command.  
 
|| In the '''Source''' window, type the following command.  
 
 
  
 
|-
 
|-
Line 163: Line 144:
 
|-
 
|-
 
|| Highlight''' inScore''' and '''ausScore''' in the '''Source''' window.
 
|| Highlight''' inScore''' and '''ausScore''' in the '''Source''' window.
 
  
 
Highlight the output in the '''Console''' 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!.'''
 
|| 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.'''
 
But no message is displayed on the '''Console.'''
Line 173: Line 152:
 
|| 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'''.  
 
|| Here the '''if''' statement will be executed only when '''India''' scores more runs than '''Australia'''.  
 
  
 
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.  
Line 219: Line 196:
 
|-
 
|-
 
|| 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.  
+
 
+
 
+
  
 
Here '''test '''is for the comparison of scores.  
 
Here '''test '''is for the comparison of scores.  
Line 233: Line 206:
 
|| Highlight '''India won the second ODI! '''in the '''Source''' window
 
|| Highlight '''India won the second ODI! '''in the '''Source''' window
 
|| * '''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.  
Line 240: Line 211:
 
|| Highlight '''Australia won the second ODI! '''in the Source window  
 
|| Highlight '''Australia won the second ODI! '''in the Source window  
 
|| * '''no''' - It returns values for false elements of '''test'''  
 
|| * '''no''' - It returns values for false elements of '''test'''  
 
 
  
 
The second statement represents the value of the false element.  
 
The second statement represents the value of the false element.  
Line 251: Line 220:
 
|| 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  
Line 271: Line 239:
 
'''}'''
 
'''}'''
 
| | 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.  
Line 315: Line 282:
 
|| Highlight the last if-else statement in the '''Source''' window  
 
|| Highlight the last if-else statement in the '''Source''' window  
 
|| A conditional structure contains, only one '''if '''statement.
 
|| 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'''.  
Line 324: Line 289:
 
|| 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.  
Line 333: Line 297:
 
|| 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 if-else statement.  
 
  
 
In the '''Source''' window, click on '''movies data frame. '''
 
In the '''Source''' window, click on '''movies data frame. '''
Line 339: Line 302:
 
|| Highlight the scroll bar in the '''Source''' window  
 
|| Highlight the scroll bar in the '''Source''' window  
 
|| 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'''.  
Line 346: Line 308:
 
|| 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  
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'''
+
* 1, if '''audience_score '''is greater than '''critics_score'''
 
* 0,''' '''otherwise'''. '''
 
* 0,''' '''otherwise'''. '''
 
  
 
|-
 
|-
Line 373: Line 334:
 
|| Highlight '''dev''' in '''movies'''  
 
|| Highlight '''dev''' in '''movies'''  
 
|| A new column named '''dev''' has been added. It has 1 or 0.  
 
|| A new column named '''dev''' has been added. It has 1 or 0.  
 
  
 
Remember, 1 means '''audience_score''' is greater than '''critics_score'''.  
 
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'''.  
 
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.
Line 403: Line 361:
  
 
Summary
 
Summary
|| In this tutorial, we have learnt about,* Conditional statements
+
|| In this tutorial, we have learnt about,
 +
* Conditional statements
 
* '''if, else, '''and '''else if '''statements
 
* '''if, else, '''and '''else if '''statements
 
  
 
|-
 
|-
Line 414: Line 372:
 
* 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 428: Line 385:
 
Spoken Tutorial Workshops
 
Spoken Tutorial Workshops
 
|| We conduct workshops using Spoken Tutorials and give certificates.
 
|| We conduct workshops using Spoken Tutorials and give certificates.
 
  
 
Please contact us.
 
Please contact us.
Line 458: Line 414:
 
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 (CoE 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.
 
|-
 
|-
 
|}
 
|}

Revision as of 16:55, 14 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, 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.

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 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 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 ODI, 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.

[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.
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 if-else 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 .

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
  • 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.

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 (CoE Pune).

This is Sudhakar Kumar from IIT Bombay signing off. Thanks for watching.

Contributors and Content Editors

Nancyvarkey, Sudhakarst