Home » How to Include NA in ifelse Statement in R

How to Include NA in ifelse Statement in R

by Tutor Aspire

Often you may want to use an ifelse statement in R to create a new column in a data frame whose values are based on the values in an existing column.

To do so, you can use the following basic syntax:

df$new_column=='A', 'val_if_true', 'val_if_false') 

However, if NA values are present in a column then the values in the new column will automatically be NA.

To avoid this, you can use the !is.na() function as follows:

df$new_column=='A' & !is.na(df$col1), 'val_if_true', 'val_if_false') 

The following example shows how to use this syntax in practice.

Example: Include NA in ifelse Statement in R

Suppose we have the following data frame in R that contains information about various basketball players:

#create data frame
df frame(player=c('A', 'B', 'C', 'D', 'E', 'F'),
                 conf=c('West', NA, 'West', 'East', 'East', 'East'),
                 points=c(30, 35, 11, 18, 14, NA))

#view data frame
df

  player conf points
1      A West     30
2      B      35
3      C West     11
4      D East     18
5      E East     14
6      F East     NA

Now suppose we attempt to create a new column called class that takes on the following values:

  • ‘West_Player’ if conf is equal to ‘West’
  • ‘Other’ if conf is not equal to ‘West’

The following code shows how to do so:

#create new column called 'class'
df$class =='West', 'West_Player', 'Other')

#view updated data frame
df

  player conf points       class
1      A West     30 West_Player
2      B      35        
3      C West     11 West_Player
4      D East     18       Other
5      E East     14       Other
6      F East     NA       Other

Notice that the value for class in row 2 is equal to NA since the corresponding value in the conf column was equal to NA.

To avoid this, we can use the !is.na() function as follows:

#create new column called 'class'
df$class =='West' & !is.na(df$conf), 'West_Player', 'Other')

#view updated data frame
df

  player conf points       class
1      A West     30 West_Player
2      B      35       Other
3      C West     11 West_Player
4      D East     18       Other
5      E East     14       Other
6      F East     NA       Other

Notice that the value for class in row 2 is now equal to ‘Other’ instead of NA.

By using the !is.na() function, we specified that the value in the conf column must be equal to ‘West’ and not equal to NA in order for the value in the new class column to be ‘West_Player’.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Write a Nested If Else Statement in R
How to Write a Case Statement in R
How to Add Multiple Columns to Data Frame in R

You may also like