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


The ifelse() 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

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

#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 here.

Leave a Reply

Your email address will not be published.