R/C2/Conditional-Statements/English-timed

From Script | Spoken-Tutorial
Jump to: navigation, search


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

Contributors and Content Editors

Sakinashaikh