How to Create Conditional Columns in R with dplyr
When you create conditional columns, you derive new values from logical tests on existing data. The dplyr package provides case_when() for multiple conditions and if_else() for a single true/false split — both operate row by row without loops and are fully vectorised for speed.
library(dplyr)
# Single condition with if_else()
df <- df |>
mutate(category = if_else(sales > 1000, "High", "Low"))
# Multiple conditions with case_when()
df <- df |>
mutate(
tier = case_when(
sales > 1000 ~ "Premium",
sales > 500 ~ "Standard",
TRUE ~ "Basic"
)
)
case_when() evaluates conditions in order and returns the first match. The final TRUE ~ "Basic" acts as the catch-all else clause. if_else() is stricter than base R’s ifelse(): it requires both branches to return the same type, which catches silent coercion bugs early. For more than two outcomes, case_when() is cleaner than chaining nested calls. When you need to create a column from multiple columns simultaneously, use case_when() inside mutate() with conditions referencing any column in the data frame. The between() helper inside case_when() simplifies range checks: case_when(between(score, 0, 50) ~ "low", between(score, 51, 80) ~ "mid", TRUE ~ "high"). For NA-safe comparisons, use %in% instead of == so that missing values do not produce unexpected results.