How to add a column to a data frame using the ifelse() function from base R, creating a categorical vector based on several logical conditions:

scores <- within(iris, {
  investigate <- ifelse(Sepal.Width > 4, "too-wide", "ok")
  investigate <- ifelse(Sepal.Length < 4.5, "too-short", investigate)
})

table(scores$investigate) # what we got
## 
##        ok too-short  too-wide 
##       143         4         3

This is how to accomplish the same using the dplyr package from the popular tidyverse:

library(dplyr)

scores <- scores %>% 
  mutate(
    investigate = case_when(
      Sepal.Width > 4 ~ "too-wide",
      Sepal.Length < 4.5 ~ "too-short",
      TRUE ~ "ok"
    )
  )

table(scores$investigate)
## 
##        ok too-short  too-wide 
##       143         4         3

A downside in base R: Multiple calls to ifelse() are necessary to realize more than two categories (“too-wide”, “too-short”, “ok”). A downside in dplyr, the default condition (the “else” branch) has to use the rather awkward TRUE ~ "ok" construction. So: Just use whatever pleases you :-)

Inspired by this blog post.


Last updated: 2020-01-15

Back to the front page