R If Else Statement

If statement

It is the most simple decision-making statement. It checks the condition if the condition is true then only it will execute the block of statements written in the ‘if statement’.

Syntax:

if (Boolean_Expression)  {      
Statement 1;     
Statement 2;     
………….    
………….     
Statement n;
}

First of all, it will check the given expression or test condition, if it is true then all the statements within the block (statement 1, 2…. n) will be executed, else all those statements will be skipped.

Flow Chart:

R if else                        

Example:

a <- 100
b <- 10
if (a > b){
  print ("A is greater than b")
}

Output:

[1] "A is greater than b"

if…else statement

If the condition is true, then if the block is executed. But if the condition is false, then else block is executed.

Syntax:

if (Boolean_Expression)  {      
Statement 1;     
Statement 2;     
………….    
………….     
Statement n;
} else {
Statement 1;     
Statement 2;     
………….    
………….     
Statement n;
}

Flow Chart:

R if else statement                        

Example:

a <- 100
b <- 200
if (a > b){
  print ("a is greater than b")
} else{
  print ("b is greater than a")
}

Output:

[1] "b is greater than a"

if…else if…else statement

It is an extension of if…statement. If the condition is true, then the statement of if block will be executed, else another if…else statement is evaluated. You may append any number of an if…else statement followed by one to another.

Syntax:

if(boolean_expression 1) {
  //Statement1
} else if( boolean_expression 2) {
  //Statement2
} else if( boolean_expression 3) {
    //Statement3
} else {
  //Statement4
}

Flow Chart:

R if-else-if                                

Example:

x <- -10
if (x < 0) {
  print("Negative number")
} else if (x > 0) {
  print("Positive number")
} else
  print("Zero")

Output:

[1] "Negative number"

Related Topics

Linear Regression in R Programming

Linear Regression is an unsupervised machine learning algorithm in R programming. Linear Regression is a type of regression analysis that shows the relationship between two or more variables. And one...

3 minutes read.

R Tutorial

Introduction to R Programming R is nothing but a programming language, also a free software environment. This software used for predictive analysis, statistical analysis, graphical representation, data modeling, and reporting. R...

9 minutes read.

Binomial Distribution in R Programming

In this article, we will talk about the Binomial distribution in R programming. The binomial distribution is a type of probability distribution. As it is a discrete distribution, it will...

3 minutes read.

R If Else Statement

If statement It is the most simple decision-making statement. It checks the condition if the condition is true then only it will execute the block of statements written in the ‘if...

2 minutes read.

Various types of Charts in R Programming

As we know, R Language is mostly used for analyzing the data and statistics purposes to represent our data graphically & pictorially in R studio. Charts and graphs are plotted in...

4 minutes read.

R Line Graphs

Line graph or line chart is a graph that connects a series of points by drawing segments between them. A line graph represents the relationship between 2 variables. These points...

2 minutes read.

Clustering in R Programming

The clustering technique is an unsupervised machine learning in R programming. Before discussing clustering techniques, let's have a look at what unsupervised machine learning is?  Unsupervised learning is training the model with...

3 minutes read.

First R Program

First  Hello World R Program As a convention, our first R program will be the “Hello World!” program. We can run our R program either at R command prompt or we...

3 minutes read.

How to make Boxplots in R

R Boxplots Boxplot is a measure of how well the data is distributed in a data set. It is used to give a summary of one or several numeric variables. The...

3 minutes read.

R Operators

A symbol that tells the compiler to perform specific mathematical or logical operations is called operator. R language supports mainly 5 different types of operators, which are listed below: Arithmetic...

3 minutes read.

Introduction to R Programming

What is R Programming? R is a programming language which provides an environment for software used for statistical analysis, graphics representation and reporting. It possesses an extensive catalog of...

4 minutes read.

R Switch Statement

It is an alternate way to a nested if…else statement. A switch statement permits an expression to be tested against a list of case values. In short, we can say...

1 minute read.

How to create Histograms in R

R Histograms A Histogram is the graphical representation of the distribution of numeric data. It takes only one numeric variable as input. The variable is cut into several bars (also called...

2 minutes read.

R Variables, Constants and Reserved Words

R Variables Variables are very similar to an open box, where you can put any value of your wish. We can change the values as well. Variables are used to store...

4 minutes read.

Decision Tree in R Programming

The decision tree uses the branching method to show every possible output for the specific input. We can draw a decision tree by hand or create it using specialized software...

2 minutes read.

R Excel Files

Microsoft Excel is the spreadsheet program which stores data in the .xls and .xlsx format. R has the facility to read directly from these files using some excel specific packages....

2 minutes read.

Normal Distribution in R Programming

In statistics, the normal distribution is a type of probability function. Normal distribution tells the user about the distribution of data values in the dataset. It is a very important...

3 minutes read.

Poisson Distribution in R Programming

Poisson distribution is a type of distribution that deals with the probability distribution of the data values by taking the mean into consideration. Poisson distribution will estimate the probability value for...

3 minutes read.

R Pie Charts

R programming language provides many numbers of libraries to create charts and graphs. A pie-chart is a representation of data as slices of a circle with different colors representing counts...

2 minutes read.

Logistic Regression in R Programming

Logistic Regression is a classification supervised machine learning algorithm in R programming. Logistic Regression can also be termed Binomial Logistic Regression, Binary Logistic Regression, or Logit Model. Logistic Regression is...

4 minutes read.