if else condition in R

If Else conditional statements are important part of any programming so as in R. In this tutorial we will have a look at how you can write a basic IF Else statement in R.  We will look at an Examples of simple if condition in R. If else condition statement, Nested if else statement, Ifelse condition of R in a dataframe. If else statement take vector as input and output a resultant vector.along with that it can also take column of the dataframe as input and results as a new column of that dataframe.

  • Example of simple If condition
  • if else condition of a vector in R
  • nested if else statement with examples
  • If else condition of a dataframe column in R along with mutate() function.

 

R if statement

The syntax of R if statement is:

if (expression)
{
statement
}

If the expression is TRUE, the statement gets executed. But if it’s FALSE, nothing happens.

Example: if statement in R

x <- 10 
if(x > 0)
{
print("This is Positive number")
}

Output

[1] “This is Positive number”

 

 

if else statement in R

The syntax of if…else statement in R is:

if (expression)
{
statement1
}
else
{
statement2
}

The else part is evaluated only when expression is FALSE.

Flow Chart :

if else statement in R 11

x <- -10 
if(x >= 0)
{
print("This is Non-negative number")
} 
else
{
print("This is Negative number")
}

Output

[1] “This is Negative number”

 

 

Nested if else statement in R

In nested if… else statements we can impose as many else if conditions as we require

The syntax of nested if…else statement is:

if (expression1)
{
statement1
}
else if (expression2)
{
statement2
}
else if (expression3)
{
statement3
}
else
statement4

Flow Chart :

if else statement in R 12

Example of nested If Else statement in R

x <- 5
if (x < 0) 
{ 
print("This is Negative number") 
} 
else if (x > 0) 
{
print("This is Positive number")
} 
else  
print("This is Zero")

Output

[1] “This is Positive number”

 

 

If else condition of a dataframe column in R:

we can also apply, if else condition to the column of the dataframe in R.  Lets see an example at first lets create a dataframe as shown below

### create a dataframe in R

df1 = data.frame(Name = c('George','Andrea', 'Micheal','Maggie','Ravi','Xien','Jalpa'), Grade_score=c(4,6,2,9,5,7,8), Mathematics_score=c(45,78,44,89,66,49,72), Science_score=c(56,52,45,88,33,90,47)) 
df1

so the resultant dataframe will be

if else condition in R 2

Now, lets use ifelse statement along with the mutate() function to get whether the they have passed or not

 
### if else condition of dataframe column  in R 

mutate(df1, Result = ifelse(Mathematics_score >= 50 & Science_score >= 50, "Pass", "Fail"))

so the resultant dataframe will Result = TRUE. If both Mathematics_score and Science_score greater than or equal to 50. If not its FALSE as shown below.

if else condition in R 4

 

 

Multiple If else condition of a dataframe column in R:

we can also apply, multiple  if else condition to the column of the dataframe in R.  Lets see an example as shown below.

 
### multiple if else condition of dataframe column  in R 

mutate(df1, Science_Grade = ifelse(Science_score %in% 50:69, "D",
                                  ifelse(Science_score %in% 70:79, "C",
                                         ifelse(Science_score %in% 80:89, "B",
                                                ifelse(Science_score %in% 90:99, "A", "F")))))


so the resultant dataframe will have grades assigned for science_score along with the mutate() function

if else condition in R 3

 

previous-small if else condition in Rnext_small if else condition in R