Introduction to Conditional Statements in R

Developed with
QA

The course is part of this learning path

Fundamentals of R
course-steps
11
certification
3
lab-steps
1
description
1
play-arrow
Introduction to Conditional Statements in R
Overview
DifficultyIntermediate
Duration25m
Students20
Ratings
5/5
starstarstarstarstar

Description

Course Description 

This module looks at more operators, and introduces conditional statements in R  

Learning Objectives 

The objectives of this module are to provide you with an understanding of: 

  • How to compare the values of two expressions  
  • How to compare the values of two Boolean expressions  
  • How to compare values of vectors  

Intended Audience 

Aimed at all who wish to learn the R programming language. 

Pre-requisites 

No prior knowledge of R is assumed 

Delegates should already be familiar with basic programming concepts such as variables, scope and functions 

Experience of another scripting language such as Python or Perl would be an advantage 

Understanding mathematical concepts will be beneficial 

 Feedback 

We welcome all feedback and suggestions - please contact us at qa.elearningadmin@qa.com to let us know what you think. 

Transcript

- [Instructor] A conditional statement is a statement that R uses to decide which code has to be run when the condition is met and for which code should not be run when the condition is not met. When a conditional statement is tested, it returns a logical element. Meaning, a true or a false. Examples of such test might include the equality symbol, the equality operator. Test of magnitude might include the greater than, or less than symbols, or the greater than or equal to, or the less than or equal to symbols. Test of inequality can be determined by the negation of the equality symbol. An IF statement in R is written with a condition in brackets followed by the action that must be performed based on the fact that the condition has been met. For example, you're browsing in a library and you decide to look at a programming textbook, and we can write an IF statement around this and say if the book genre that has been defined by the one that you've found in the library is programming, then you will perform an action. The action happens to be a print statement calling "it was programming to the screen" which we can see here because we knew that we have defined our book genre as such. If for example I had written or found a book about painting and then run the same IF statement again, at this point here, the condition would no longer be true, and hence, the performance of the action would no longer occur. And as you can see, nothing has been returned whilst before we saw the printed statement on the screen. We can extend the IF statement to include an ELSE clause. In this since here I have created a template for an IF/ELSE statement, at this point here, I can say this is exactly the same that we had in the previous screen, but we had if the condition is true, we can perform an action. I would like to add in else. Meaning, if this condition is not true and in the sense of our book genre where we have a non-programming textbook, you would perform this alternative action. So for example, had I gone to the library and picked up a book about painting followed by a book about programming and I was to run an IF/ELSE statement around this where if the book genre was programming, it would return "it was programming", otherwise, it would return "it was painting". This would check the last book, "it was programming", because the last book was programming. Now, let's say for example, I had switched these two things around and decided to pick up the painting book second or last by running the IF statement, I now decide by looking at the condition, verifying if it is true or not and in this instance, it is not true, so I would perform the else step which is the printing to the output device that "it was painting" and, hence, we see the output, "it was painting" at the bottom of the screen. I can have more than one line in each of my code chunks. Say for example, note in the Global Environment that the last book that I picked up was a painting book, I could do many calculations based on the fact that I have now proof stated that the last book that I've picked up was not a programming book, so I would enter the ELSE block of code and this would print to the screen, "it was painting" and do some calculations. This variable here can do calculations would be created inside the Global Environment and as you can see, it was created. Had I for example, picked up a programming book and rerun this IF statement such that the last book that I've read was a programming book, then the condition here of if book genre is equal to programming, we would then run this code block here. This is then created a variable called can assign values within the block of code for the two printed statements which state that "it was programming" which we understood from our condition being true and "can be more than one line" to indicate that this code block has more than one line of code. And I've also added a variable called can assign values and, hence, I picked up the text string that was inside of book genre which was defined outside of the IF/ELSE statement. I can do more complicated and nested IF/ELSE statements such as following where I have an IF statement at the start, so as we've defined so far. I can also have another IF statement here which I have highlighted as being the second nested IF statement and then I can clause this off with an else at the end for any other case where I say, "who knows?". At this point here, about to note that the book genre last looked up was programming, so we would only be seeing the first IF statement, rather than the rest of the ELSE clauses. So we would only be printing, "it was programming" to the screen. If, however, say I had chosen a book on painting as my last book to read and rerun this IF statement, I would then pick up the second print statement, "it was painting" and that is what's been outputted to the screen. However, let's say I've picked up a third book and this was my last book before I finished and if I rerun the IF/ELSE statement, I would see the final clause of printing or the final code block being run which could have any number of lines, but in this since I have written "who knows?" onto the screen.

About the Author
Students363
Labs1
Courses11
Learning paths1

Kunal has worked with data for most of his career, ranging from diffusion markov chain processes to migrating reporting platforms.  

Kunal has helped clients with early stage engagement and formed multi week training programme curriculum. 

Kunal has a passion for statistics and data; he has delivered training relating to Hypothesis Testing, Exploring Data, Machine Learning Algorithms, and the Theory of Visualisation. 

Data Scientist at a credit management company; applied statistical analysis to distressed portfolios. 

Business Data Analyst at an investment bank; project to overhaul the legacy reporting and analytics platform. 

Statistician within the Government Statistical Service; quantitative analysis and publishing statistical findings of emerging levels of council tax data. 

Structured Credit Product Control at an investment bank; developing, maintaining, and deploying a PnL platform for the CVA Hedging trading desk.