Please disable your adblock and script blockers to view this page

Search this blog

Monday, 13 August 2018

R Decision Making - IF...ELSE Statement in R Programming

We can write a simple IF statement, IF...ELSE statements or nested IF statements as per our requirement. If you know the 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



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)
}ELSE
Statement to execute (if condition 1& 2 both are false)

For a 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

No comments :

Post a Comment