EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials R Programming Tutorial If Else Statement in R

If Else Statement in R

By Afshan BanuAfshan Banu

If Else Statement in R

Introduction to If Else Statement in R

If Else Statement in R language is a control statement that is used to check the logical condition and process the R statements inside the if block scope in case the condition returns a true boolean value and in the else block scope in case the condition expression returns a false boolean value.The if statements support multiple else statements as a part of complex logical scenarios in R language. There is a built-in function in R language known as ifelse(), which accepts condition expression, and R statements for if and else scenarios. The if-else statement in R is very useful and easy to use for statement selection and data processing while developing the program logic.

The Syntax of the if Statement

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

If ( cond ) {
Statement 1
Statement 2
Statement n
}

If cond is true, then all the statements inside the body are executed; else, nothing will be executed (no output). The cond is the condition that can be a numeric or logical vector. The all-numeric vector takes as TRUE except zero and takes the logical vector, a TRUE vector or FALSE vector.

The Flow Diagram of the if statement –

Flow Diagram of the if statement

How does the if statement works?

Let us see some examples for if statement

Example #1

Code:

no <- 10
if (no > 0) {
print(" Number is positive ")
}

Output:

If Else Statement in R 1 output

The above code checks whether the no is greater than zero; if true, then print ” Number is positive, “else nothing.

As in the above code, if the no is not greater than zero, nothing is performed, but if we need to perform or execute soothing, we can the else body.

The Syntax of the if-else statement

if ( cond ) {
statement 1
} else {
Statement 2
}

If cond is true, then all the statements inside the body are executed; else, if the cond is false, then all statements inside the else body are executed.

The important note here is the else should be in the same line as the if statement closing braces.

The Flow Diagram of the if-else statement

Flow Diagram of the if-else statement 2

Example #2

The R code example to see how the if-else statement works with no = 10

Code:

no <- 10
if (no > 0) {
print(" Number is positive ")
} else {
print(" Number is Negative ")
}

Output: 

If Else Statement in R 2 output

Example #3

R code example to see how the if-else statement works with no = -10

Code:

no <- -10
if (no > 0) {
print(" Number is positive ")
} else {
print(" Number is Negative ")
}

Output:

If Else Statement in R 3 output

Example #4

The above R code can be rewritten as a ternary operator

no <- -10
if (no > 0) print(" Number is positive ") else print(" Number is Negative ")

The if-else if statement

If not only one condition, if multiple conditions to be check and base on the specific condition the specific statement or statements to be executed, then the if-else if statement can be used.

The Syntax of the if-else if statement

if ( cond1 ) {
statement 1
} else if ( cond2 ) {
Statement 2
} else if (cond3 ) {
Statement 3
} else {
Statement 4
}

The if statement checks the condition, but if more than one or alternative conditions are to check, the alternative conditions can be checked with else if statement. Among all condition, only one condition gets true, so only one statement gets to execute.

Example #5

Code:

no <- 6
if (no < 0) {
print(" Number is negative ")
} else if (no > 10) {
print(" Number is positive and greater than 10 ")
} else
print(" Number is positive and less than 10 ")

Output:

If Else Statement in R 4 output

Nested if statement

The if statement can have another if statement or even if-else inside it; it is called nested if statement and not only in if, the else part also can have nested if also nested if-else statement.

R code example where first it checks whether the no is positive or not and if the no is positive, it will check whether the no is even or not. For example, we take the no value as 6, so the no is positive.

Example #6

Code:

no <- 6
if( no>0 ){
print(" Number is positive ")
if( no %% 2==0 ){
print(" and Number is even ")
}
}

Output:

program 5 output

The output if no=5 

program 6 output

So the output is displaying only the number is positive.

Next, we see some of the R program examples with the if-else statement; first, we write the program to print the bigger no.

Example #7

Code:

a <- as.integer(readline(prompt="Enter a value "))
b <- as.integer(readline(prompt="Enter b value "))
if( a>b ){
print(" a is greater than b ")
} else {
print(" b is greater than a ")
}

Output:

program 7 output

Example #8

Program to accept the marks and display the code as if the mark is in between 100 and 80 then grade A, else if mark between 80 and 50 then B, else grade C.

Code:

mark <- as.integer(readline(prompt="Enter mark "))
if( mark<=100 && mark>80 ){
print(" Your grade is A ")
} else if ( mark<=80 && mark>50 ) {
print(" Your grade is B ")
} else {
print(" Your grade is C")
}

Output:

program 8 output

Example #9

Program to check whether enter character is upper case letter or lower case letter.

Code:

ch <- readline(prompt=" Enter Character ")
if( ch>='A' && ch<='Z' ){
print(" Entered character is upper case ")
} else if ( ch>='a' && ch<='z' ) {
print(" Entered character is lower case ")
} else {
print(" You have not entered a Character ")
}

Output:

program 9 output

Conclusion

The if-else statement is the conditional construct in which the sequence of execution of statements decides based on the condition. In if statement, if cond is true, then all the statements inside the body executed, else nothing will be executed (no output). The cond is the condition that can be a numeric or logical vector. An if-else statement, if cond is true, then all the statements inside the body are executed, else if the cond is false, then all statements inside the else body are executed.

The if-else if statement used to check the multiple conditions. The if statement can have another if statement or even if – else inside it or and else can have inside another if-else statement, it is called nested if statement.

Recommended Articles

This is a guide to If Else Statement in R. Here, we discuss the R code examples to see how the if statement works with the programs and outputs. You may also look at the following article to learn more –

  1. If Else Statement in Python
  2. If-else Statement in C
  3. C# if Statement
  4. Tableau IF Statement
All in One Excel VBA Bundle
500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access
Financial Analyst Masters Training Program
1000+ Hours of HD Videos
43 Learning Paths
250+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Data Science Bundle
1500+ Hour of HD Videos
80 Learning Paths
360+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Software Development Bundle
3000+ Hours of HD Videos
149 Learning Paths
600+ Courses
Verifiable Certificate of Completion
Lifetime Access
Primary Sidebar
All in One Data Science Bundle1500+ Hour of HD Videos | 80 Learning Paths | 360+ Courses | Verifiable Certificate of Completion | Lifetime Access
Financial Analyst Masters Training Program1000+ Hours of HD Videos | 43 Learning Paths | 250+ Courses | Verifiable Certificate of Completion | Lifetime Access
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Hurry! - Any Learning Path @ $19 | OFFER ENDING IN ENROLL NOW