(R Tutorials for Business Analyst)
IF, ELSE, ELSE IF Statement in R
The if else statement
An if-else statement is a great tool for the developer trying to return an output based on a condition. In R, the syntax is:
if (condition) { Expr1 } else { Expr2 }
We want to examine whether a variable stored as “quantity” is above 20. If quantity is greater than 20, the code will print “You sold a lot!” otherwise Not enough for today.
# Create vector quantity quantity <- 25 # Set the is-else statement if (quantity > 20) { print('You sold a lot!') } else { print('Not enough for today') }
Output:
## [1] "You sold a lot!"
Note: Make sure you correctly write the indentations. Code with multiple conditions can become unreadable when the indentations are not in correct position.
Python tutorials for Business Analyst – Python pass statement
The else if statement
We can further customize the control level with the else if statement. With elif, you can add as many conditions as we want. The syntax is:
if (condition1) { expr1 } else if (condition2) { expr2 } else if (condition3) { expr3 } else { expr4 }
We are interested to know if we sold quantities between 20 and 30. If we do, then the pint Average day. If quantity is > 30 we print What a great day!, otherwise Not enough for today.
You can try to change the amount of quantity.
# Create vector quantiy quantity <- 10 # Create multiple condition statement if (quantity <20) { print('Not enough for today') } else if (quantity > 20 &quantity <= 30) { print('Average day') } else { print('What a great day!') }
Output:
## [1] "Not enough for today"
Example 2:
VAT has different rate according to the product purchased. Imagine we have three different kind of products with different VAT applied:
Categories | Products | VAT |
---|---|---|
A | Book, magazine, newspaper, etc.. | 8% |
B | Vegetable, meat, beverage, etc.. | 10% |
C | Tee-shirt, jean, pant, etc.. | 20% |
We can write a chain to apply the correct VAT rate to the product a customer bought.
category <- 'A' price <- 10 if (category =='A'){ cat('A vat rate of 8% is applied.','The total price is',price *1.08) } else if (category =='B'){ cat('A vat rate of 10% is applied.','The total price is',price *1.10) } else { cat('A vat rate of 20% is applied.','The total price is',price *1.20) }
Output:
# A vat rate of 8% is applied. The total price is 10.8
Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.
Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners
Latest end-to-end Learn by Coding Projects (Jupyter Notebooks) in Python and R:
All Notebooks in One Bundle: Data Science Recipes and Examples in Python & R.
End-to-End Python Machine Learning Recipes & Examples.
End-to-End R Machine Learning Recipes & Examples.
Applied Statistics with R for Beginners and Business Professionals
Data Science and Machine Learning Projects in Python: Tabular Data Analytics
Data Science and Machine Learning Projects in R: Tabular Data Analytics
Python Machine Learning & Data Science Recipes: Learn by Coding
R Machine Learning & Data Science Recipes: Learn by Coding
Comparing Different Machine Learning Algorithms in Python for Classification (FREE)
There are 2000+ End-to-End Python & R Notebooks are available to build Professional Portfolio as a Data Scientist and/or Machine Learning Specialist. All Notebooks are only $29.95. We would like to request you to have a look at the website for FREE the end-to-end notebooks, and then decide whether you would like to purchase or not.
R tutorials for Business Analyst – R Data Frame: Create, Append, Select, Subset
Excel formula for Beginners – How to SUMPRODUCT with IF in Excel
Python tutorials for Business Analyst – How to get current date and time in Python?