R If Else | R If Else Statement - r - learn r - r programming



  • This can be achieved in R programming using the conditional if-else statement.
  • If-else returns a value with the same condition as test which is filled with elements selected from either yes or no depending on whether the element of test is TRUE or FALSE.
if else while loop

Read Also

Syntax

if (test_expression) 
{
   statement1
} 
else
{
   statement2
}

Syntax Explanation:

  • test_expression: If the test_expression is TRUE, the statement gets executed. But if statement is FALSE, nothing will occur.
  • else: Here else part is an optional. It is evaluated if test_expression is FALSE. The else must be in the same line as the closing braces of the if statements.
r if elseif

Sample Code

x <- 5
if(x > 2)
 {
   print("This is even number")
 }
else
 {
   print("This is odd number")
 }
 

Read Also

Code Explanation

 r ifelse code
  1. Here we declare the variable x value as “5”.
  2. In this statement we check the condition that “x” value is greater than “2”, if condition satisfies it prints "This is even number”. If the condition is FALSE, its go to else statement.

Read Also

Output

 if else code output
  1. Here in this output we enter a value of the variable x= 5 so the given number is greater than “2” so the “if” section will be executed by displaying the output as “This is even number”.


Related Searches to R If Else | R If Else Statement