2.9 Conditional calculations - if_else()

And finally, we combine the filtering operators (==, >, <, etc) with the if_else() function to create new columns based on a condition.

typesdata %>% 
  mutate(above_threshold = if_else(measurement > 3,
                                   "Above three",
                                   "Below three"))
## # A tibble: 3 x 5
##   id    group     measurement date                above_threshold
##   <chr> <chr>           <dbl> <dttm>              <chr>          
## 1 ID1   Control           1.8 2017-01-02 12:00:00 Below three    
## 2 ID2   Treatment         4.5 2018-02-03 13:00:00 Above three    
## 3 ID3   Treatment         3.7 2019-03-04 14:00:00 Above three

We are sending typesdata into a mutate() function, we are creating a new column called above_threshold based on whether measurement is greater or less than 3. The first argument to if_else() is a condition (in this case that measurement is greater than 3), the second argument is the value if the condition is TRUE, and the third argument is the value if the condition is FALSE.

It reads, “if this condition is met, return this, else return that”.

Look at each line in the tibble above and convince yourself that the threshold variable worked as expected. Then look at the two closing brackets - )) - at the end and convince yourself that they are both needed.

if_else() and missing values tip: for rows with missing values (NAs), the condition returns neither TRUE nor FALSE, it returns NA. And that might be fine, but if you want to assign a specific group/label for missing values in the new variable, you can add a fourth argument to if_else(), e.g., if_else(measurement > 3, "Above three", "Below three", "Value missing").