# Beginners Guide to R – R if else elseif Statement

Often, you need to execute some statements only when some condition is met. You can use following conditional statements in your code to do this.

• `if` Statement: use it to execute a block of code, if a specified condition is true
• `else` Statement: use it to execute a block of code, if the same condition is false
• `else if` Statement: use it to specify a new condition to test, if the first condition is false
• `ifelse()` Function: use it when to check the condition for every element of a vector

## The if Statement

Use `if` statement to execute a block of code, if the condition is true.

### Making a simple comparison

``````x <- 7
y <- 5
if(x > y) {
print("x is greater")
}
 "x is greater"``````

Likewise, you can use following comparison operators to compare two values:

 Operator Meaning Example == Equals if (x == y) != Not equals if (x != y) > Greater than if (x > y) >= Greater than or equal to if (x >= y) < Less than if (x < y) <= Less than or equal to if (x <= y)

### More Examples

In R, any non-zero value is considered TRUE, whereas a zero is considered FALSE. That’s why all the below if statements are valid.

``````# mathematical expression
x <- 7
y <- 5
if(x + y) {
print("True")
}
 "True"

# any non-zero value
if(-3) {
print("True")
}
 "True"``````

## if Statement Without Curly Braces

If you have only one statement to execute, you can skip curly braces.

``````x <- 7
y <- 5
if(x > y) print("x is greater")
 "x is greater"``````

## Nested if Statement

You can write one `if` statement inside another `if` statement to test more than one condition and return different results.

``````x <- 7
y <- 5
z <- 2
if(x > y) {
print("x is greater than y")
if(x > z) print("x is greater than y and z")
}
 "x is greater than y"
 "x is greater than y and z"``````

## The else Statement

Use `else` statement to execute a block of code, if the condition is false.

### A Simple if-else comparison

``````x <- 7
y <- 5
if(x > y) {
print("x is greater")
} else {
print("y is greater")
}
 "x is greater"``````

## The else if Statement

Use `else if` statement to specify a new condition to test, if the first condition is false.

### Using else-if Statement

``````x <- 5
y <- 5
if(x > y) {
print("x is greater")
} else if(x < y) {
print("y is greater")
} else {
print("x and y are equal")
}
 "x and y are equal"``````

In R, you can use as many `else if` statements as you want in your program. There’s no limit. However, it’s not a best practice when you want to make series of decisions. You can use switch() function as an efficient way.

## Multiple Conditions

To join two or more conditions into a single `if` statement, use logical operators viz. `&&` (and), `||` (or) and `!` (not).

`&&` (and) expression is True, if all the conditions are true.

``````x <- 7
y <- 5
z <- 2
if(x > y && x > z) {
print("x is greater")
}
 "x is greater"``````

`||` (or) expression is True, if at least one of the conditions is True.

``````x <- 7
y <- 5
z <- 9
if(x > y || x > z) {
print("x is greater than y or z")
}
 "x is greater than y or z"``````

`!` (not) expression is True, if the condition is false.

``````x <- 7
y <- 5
if(!(x < y)) {
print("x is greater")
}
 "x is greater"``````

## One Line If…Else

If you have only one statement to execute, one for if , and one for else , you can put it all on the same line:

### Examples

``````x <- 7
y <- 5
if (x > y) print("x is greater") else print("y is greater")
 "x is greater"``````

You can also use it to select variable assignment.

``````x <- 7
y <- 5
max <- if (x > y) x else y
max
 7``````

## The ifelse() Function

In R, conditional statements are not vector operations. They deal only with a single value.

If you pass in, for example, a vector, the `if` statement will only check the very first element and issue a warning.

``````v <- 1:6
if(v %% 2) {
print("odd")
} else {
print("even")
}
 "odd"
Warning message:
In if (v%%2) { :
the condition has length > 1 and only the first element will be used``````

The solution to this is the `ifelse()` function. The `ifelse()` function checks the condition for every element of a vector and selects elements from the specified vector depending upon the result.

Here’s the syntax for the `ifelse()` function.

### Examples

``````v <- c(1,2,3,4,5,6)
ifelse(v %% 2 == 0, "even", "odd")
 "odd"  "even" "odd"  "even" "odd"  "even"``````

You can even use this function to choose values from two vectors.

``````v1 <- c(1,2,3,4,5,6)
v2 <- c("a","b","c","d","e","f")
ifelse(c(TRUE,FALSE,TRUE,FALSE,TRUE,FALSE), v1, v2)
 "1" "b" "3" "d" "5" "f"``````

# Special 95% discount

## 2000+ Applied Machine Learning & Data Science Recipes

### Portfolio Projects for Aspiring Data Scientists: Tabular Text & Image Data Analytics as well as Time Series Forecasting in Python & R ## Two Machine Learning Fields

There are two sides to machine learning:

• Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
• Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.

Data Science Resources: Data Science Recipes and Applied Machine Learning Recipes

Introduction to Applied Machine Learning & Data Science for Beginners, Business Analysts, Students, Researchers and Freelancers with Python & R Codes @ Western Australian Center for Applied Machine Learning & Data Science (WACAMLDS) !!!

Latest end-to-end Learn by Coding Recipes in Project-Based Learning:

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)

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