if else in R

In this post, we will learn about if else in R language.

if else is  also called a conditional expression.

A conditional expression evaluates a logical condition and then proceeds to run the expression only if the logical condition is true.

if statement in R

Refer to the syntax of if statement in R,  below

if (condition) {
expression
}

condition can be a logical vector or a numeric vector of length one. If the condition vector has more than 1 element, then only first element is taken for evaluation.

if statement in R example

Let us understand if statement in R with example.

R if statement example

Before executing the if statement, a variable y is assigned the numeric value 6.

y > 5 condition will return a logical vector containing a single element TRUE.

Hence R will  print “Greater than 5” to the console.

What if the condition in the R if statement returns a logical vector of more than 1 element ?

A warning message is printed , and only first element is considered for evaluation.

In the below if statement in R example, as the first element of the logical vector returned by vect > 1 is FALSE, the message is not printed.

R if statement example 2

In R, a boolean TRUE is considered equal to numeric 1. Hence the print message worked in the example given below.

if statement in RLet us understand the scenario, when condition within R if statement is  a  numeric vector.

In the below example, vect3 is a numeric vector.

Since the 1st element of vect3 is 1, and equivalent to TRUE, it will print the Test message.

Since there are multiple numeric elements here, R will print a warning message as well.

R if statement example 3

If the 1st element in condition vector is 0, and 0 is equivalent to FALSE, the print statement will not be executed here.

R if statement example 4

if else in R

Refer to the syntax of if else in R, below.

if (condition) {
expression1
} else {
expression2
}

else is an optional clause.You can use an else statement only in combination with an if statement.

If you want to execute an expression when the condition in preceding if statement is  FALSE, then use an if else in R.

The else statement does not require a condition.

Please note that the else keyword should come on the same line as the closing bracket of the if part!

if else in R example

Take a look at the below example of if else statement in R

As the value of variable y is 4 and 4 is less than 5, the condition y > 5 becomes false, and  else statement gets executed in the below code.

R if else statement example

R else if

The R else if statement allows you to further customise your R code.

Take a look at the syntax of R else if statement.

Syntax

if (condition1) {
expression1
} else if (condition2) {
expression2
} else if (condition3) {
expression3
} else {
expression4
}

The control will go to the first else if statement (which evaluates condition 2) only when the preceding if statement ‘s condition1 is false.

The subsequent else if statement(which evaluates condition3) will get executed only when the preceding else if statement’s condition2 is false .

You can add as many R else if statements as you like.There is no limitation

Again, it’s important that the else if keyword comes on the same line as the closing bracket of the previous part of the control structure.

R else if statement example

Let’s understand R else if statement with an example.

In the below code snippet , y’s value is 4.5. Since 4.5 is less than 5, the if statement wont be executed.

The control will next go to the first else if statement which checks whether y > 4.

Since the condition for this else if statement is TRUE , the last else statement will not be executed.

R else if statement example

R ifelse() function

ifelse() function is the vectorized form of the R if else statement.

A vectorized operation is much faster than normal operation, as vectorized operations work at vector level rather than repeating the same operation for each individual element of a vector.

Syntax of R ifelse

ifelse(expression, x, y)

Here the expression should be a logical vector. The return value from executing ifelse function is a vector of same length as the logical vector.

R ifelse function example

Let us understand R ifelse function with an example.

ifelse function in R

I am passing vect %%3 == 0 as an expression. Each element of the vector vect will be divided by 3 and checked whether the remainder will be zero.

Hence , this will expression will yield a logical vector c(TRUE,TRUE,FALSE,TRUE) , containing 4 boolean values.

The ifelse function’s first value , namely “Divisible by 3”,  will be recycled to form a string vector containing 4 values -c(“Divisible by 3″,”Divisible by 3″,”Divisible by 3″,”Divisible by 3”)

Same logic applies for the ifelse function’s second value, namely  “Not Divisible by 3”.This will be recycled to c(“Not Divisible by 3″,”Not Divisible by 3″,”Not Divisible by 3″,”Not Divisible by 3”).

Wherever the logical vector element is TRUE, the corresponding element from the recycled x will be returned -“Divisible by 3” in this example.

Wherever the logical vector is FALSE, the corresponding element from the recycled y will be returned – “Not Divisible by 3” in this example.

Hope you are now clear with if statement, if else statement, else statement and ifelse function in R.

We will discuss about further R topics in my data science course in hyderabad. Feel free to ask your questions in the comments section below.

 

 

Recent Posts

Menu