Cancel Preloader

Teaching R decision making in simple language

Decision structures require the programmer to specify one or more conditions that must be estimated or tested by the program, if this condition is correct; 

Decision , Command or commands must be executed; And you can write commands as you wish, if those conditions are not met; Be run.

Below you can see the general form of a decision structure found in most programming languages:

decision

The R environment provides the following types of decision commands. For more details you can click on the links:

Row Commands and descriptions 
1 Command ifAn if statement contains a boolean statement followed by one or more statements.
۲ Command if… elseAn if statement can be followed by an else statement ; Which is executed when the Boolean expression is false.
3 Switch commandA switch statement allows a variable to be compared to a list of variables in terms of parity.

 

It is one of the control statements in R programming that consists of a Boolean expression and a set of statements. If the Boolean expression evaluates to TRUE, the set of statements is executed. If the Boolean expression evaluates to FALSE, the statements after the end of the If statement are executed.

The basic syntax for the If statement is given below:

if(Boolean_expression) {
This block of code will execute if the Boolean expression returns TRUE.
}

For example:

x <- “Intellipaat”
if(is.character(x)) {  
print("X is a Character")
}

Output:[1] “X is a Character”

 

Else Statement

In the If -Else statement, an If statement is followed by an Else statement, which contains a block of code to be executed when the Boolean expression in the If the statement evaluates to FALSE.
The basic syntax of it is given below:

if(Boolean_expression) {  
This block of code executes if the Boolean expression returns TRUE.
} else {  
This block of code executes if the Boolean expression returns FALSE.
}

For example:

x <- c("Intellipaat","R","Tutorial")
if("Intellipaat" %in% x) {  
print("Intellipaat")
} else {  
print("Not found")
}

Output:  [1] “Intellipaat”

 

Leave a Reply

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