R Decision Making – IF…ELSE Statement in R Programming

Sharing is Caring

Like many other programming languages, R also supports decision making statements, These statements are also called a conditional statement. Conditional statements work on basis of a predefined condition’s output. R decision making helps a programmer to write code efficiently.

We can write a simple IF statement, IF…ELSE statements or nested IF statements as per our requirement. If you know basics of any other programming language then after this tutorial you’ll get an idea about writing conditional statements in R programming.

Flow Chart of IF…ELSE is like this

If Else - R Decision Making

Basic Syntax of IF-ELSE is like this in R

IF (Condition 1){

Statement to execute (if condition 1 is true)

}ELSE IF (Condition 2){

Statement to execute (if condition 2 is true)

Statement to execute (if condition 1& 2 both are false)

For the better understanding of decision-making, statements see this example

> #Declare Variables
> a <- 20
> b <- 40
> c <- 5
> #Check conditions
> if(a>b && a>c){
+ print("Largest Value is A")
+ }else if(b>a && b>c){
+ print("Largest Value is B")
+ }else print("Largest Value is C-")

[1] "Largest Value is B"

This is how IF…ELSE works in R programming.

Cheers 🙂 Happy Learning

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *