How can you write a nested if-else statement in R with examples?

A nested if-else statement in R is a conditional statement that allows for multiple levels of decision-making. It consists of one or more if statements within another if statement. This structure allows for more complex logic and can be useful in situations with multiple conditions.

An example of a nested if-else statement in R would be:

if (x > 0) {
if (y > 0) {
print(“Both x and y are positive.”)
} else {
print(“x is positive, but y is not.”)
}
} else {
print(“x is not positive.”)
}

In this example, the first if statement checks if the value of x is greater than 0. If it is, then the code inside the curly braces will be executed, which includes a second if statement that checks if the value of y is also greater than 0. If both conditions are met, the message “Both x and y are positive” will be printed. If the second condition is not met, the else statement will execute and print “x is positive, but y is not.” If the first condition is not met, the else statement will execute and print “x is not positive.”

Nested if-else statements can also be used with more than two conditions, by including additional if statements within the else statement. It is important to properly structure and indent the code to avoid confusion and errors.

Write a Nested If Else Statement in R (With Examples)


The function in base R can be used to write quick if-else statements. This function uses the following syntax:

ifelse(test, yes, no)

where:

  • test: A logical test
  • yes: The value to return if the logical test is True
  • no: The value to return if the logical test is False

This tutorial explains how to use this function to write if else statements along with nested if else statements in R, using the following data frame:

#create data frame
df <- data.frame(team = c('A', 'A', 'B', 'B', 'B', 'C', 'D'),
                 points = c(4, 7, 8, 8, 8, 9, 12),
                 rebounds = c(3, 3, 4, 4, 6, 7, 7))

#view data frame
df

  team points rebounds
1    A      4        3
2    A      7        3
3    B      8        4
4    B      8        4
5    B      8        6
6    C      9        7
7    D     12        7

Example 1: How to Write a Basic If Else Statement

The following code shows how to create a new column in the data frame whose value is based off the value in the ‘team’ column:

#create new column in data frame
df$rating <- ifelse(df$team == 'A', 'great', 'bad')

#view data frame
df

  team points rebounds rating
1    A      4        3  great
2    A      7        3  great
3    B      8        4    bad
4    B      8        4    bad
5    B      8        6    bad
6    C      9        7    bad
7    D     12        7    bad

This simple ifelse statement tells R to do the following:

  • If the value in the team column is ‘A’ then give the player a rating of ‘great.’
  • Else, give the player a rating of ‘bad.’

Example 2: How to Write a Nested If Else Statement

The following code shows how to create a new column in the data frame by writing a nested if else statement:

#create new column in data frame
df$rating <- ifelse(df$team == 'A', 'great',
               ifelse(df$team == 'B', 'OK', 'bad'))

#view data frame
df

  team points rebounds rating
1    A      4        3  great
2    A      7        3  great
3    B      8        4     OK
4    B      8        4     OK
5    B      8        6     OK
6    C      9        7    bad
7    D     12        7    bad

This nested ifelse statement tells R to do the following:

  • If the value in the team column is ‘A’ then give the player a rating of ‘great.’
  • Else, if the value in the team column is ‘B’ then give the player a rating of ‘OK.’
  • Else, give the player a rating of ‘bad.’

Example 3: How to Write Longer Nested If Else Statements

#create new column in data frame
df$rating <- ifelse(df$team == 'A', 'great',
               ifelse(df$team == 'B', 'OK',
                 ifelse(df$team == 'C', 'decent', 'bad')))

#view data frame
df

  team points rebounds rating
1    A      4        3  great
2    A      7        3  great
3    B      8        4     OK
4    B      8        4     OK
5    B      8        6     OK
6    C      9        7 decent
7    D     12        7    bad

This nested ifelse statement tells R to do the following:

  • If the value in the team column is ‘A’ then give the player a rating of ‘great.’
  • Else, if the value in the team column is ‘B’ then give the player a rating of ‘OK.’
  • Else, if the value in the team column is ‘C’ then give the player a rating of ‘decent.’
  • Else, give the player a rating of ‘bad.’

Note that you can use this exact pattern to write nested ifelse statements as long as you’d like.

You can find more R tutorials .

x