dplyr::if_else()
   get_help() docs


Description

The if_else() function is part of the {dplyr} package, which is part of the {tidyverse}.

We use this function to assign a value based on whether a logical statement is TRUE or FALSE. We often use if_else() along with mutate() to create new data frame columns. The provided values must be of the same type!

Note that there is a Base R version of this function dplyr::ifelse() (without an underscore!) that can be used in exactly the same way, but the Base version does not enforce that the result is always the same type.

To use this function, you need to either first load the {dplyr} library, or always use the function with dplyr::if_else() notation.

# Load the library
library(dplyr)
# Or, load the full tidyverse:
library(tidyverse)

# Or, use :: notation
dplyr::if_else()

Conceptual Usage

if_else(logical statement, 
        value if statement is TRUE, 
        value if statement is FALSE)

tibble %>%
  mutate(new_column = if_else(logical statement, 
                              value for new_column if statement is TRUE, 
                              value for new_column if statement is FALSE)
  )

Examples

Some examples below use the carnivores dataset. Learn more about this dataset with get_help("carnivores").

# Show the carnivores dataset
carnivores
## # A tibble: 9 × 4
##   name              genus        awake brainwt
##   <chr>             <fct>        <dbl>   <dbl>
## 1 Arctic fox        Vulpes        11.5  0.0445
## 2 Cheetah           Acinonyx      11.9 NA     
## 3 Dog               Canis         13.9  0.07  
## 4 Gray seal         Haliochoerus  17.8  0.325 
## 5 Jaguar            Panthera      13.6  0.157 
## 6 Lion              Panthera      10.5 NA     
## 7 Northern fur seal Callorhinus   15.3 NA     
## 8 Red fox           Vulpes        14.2  0.0504
## 9 Tiger             Panthera       8.2 NA


# Return the value 5 if it is TRUE that 10 < 4. Otherwise, return the value 7.
if_else(10 < 4, 5, 7)
## [1] 7


# Return the value "hello" if the first argument is true, and "goodbye" otherwise
if_else(nchar("aloha") == 10, "hello", "goodbye")
## [1] "goodbye"


# This example's logic is equivalent to the previous one
if_else(nchar("aloha") != 10, "goodbye", "hello")
## [1] "goodbye"


## CAUTION! This code will result in an ERROR since (what to do if TRUE) 5 is a number, but
## 'string' (what ro do if FALSE) is character. Those are different types.
if_else(10 < 100, 5, 'string')
## Error in `glubort()`:
## ! `false` must be a double vector, not a character vector.


# Use if_else to help create a new column called `how_awake` in `carnivores` that will be:
## "alert" if awake >= 15 and "sleepy" otherwise.
carnivores %>%
  mutate(how_awake = if_else(awake >= 15, "alert", "sleepy"))
## # A tibble: 9 × 5
##   name              genus        awake brainwt how_awake
##   <chr>             <fct>        <dbl>   <dbl> <chr>    
## 1 Arctic fox        Vulpes        11.5  0.0445 sleepy   
## 2 Cheetah           Acinonyx      11.9 NA      sleepy   
## 3 Dog               Canis         13.9  0.07   sleepy   
## 4 Gray seal         Haliochoerus  17.8  0.325  alert    
## 5 Jaguar            Panthera      13.6  0.157  sleepy   
## 6 Lion              Panthera      10.5 NA      sleepy   
## 7 Northern fur seal Callorhinus   15.3 NA      alert    
## 8 Red fox           Vulpes        14.2  0.0504 sleepy   
## 9 Tiger             Panthera       8.2 NA      sleepy