rguides

How to Subset Data Frames by Multiple Conditions in R

Subset data frames by multiple conditions to filter rows based on several criteria at once. Use & for AND (both true), | for OR (at least one true), and ! for NOT (negates). The same logical operators work across base R, dplyr, and data.table with only syntactic differences. Always wrap mixed AND/OR logic in parentheses to control evaluation order.

library(dplyr)

# AND: both conditions must be TRUE
df %>% filter(salary > 50000 & department == "Engineering")

# OR with parentheses for grouping
df %>% filter((salary > 50000 | bonus > 10000) & department == "Sales")

# NOT: exclude values
df %>% filter(!department %in% c("HR", "Legal"))

Base R uses the same operators with square brackets — remember the comma after conditions for data frames: df[df$salary > 50000 & df$department == "Engineering", ]. For data.table, the syntax is more concise: dt[salary > 50000 & department == "Engineering"].

# Base R: numeric range with between()
df %>% filter(between(age, 25, 45))

# String matching with %in%
df %>% filter(city %in% c("New York", "Los Angeles"))

Without parentheses, salary > 50000 & department == "Sales" | department == "Engineering" evaluates differently than intended because & has higher precedence than |. For excluding multiple values, %in% combined with ! is cleaner than chaining several != checks. Base R also provides subset(df, salary > 50000 & department == "Engineering") as a more readable alternative to bracket notation, though dplyr’s filter() is typically preferred for pipelines.

See also

  • filter(), dplyr filtering verb
  • ifelse(), Conditional vectorized if-else
  • which(), Find indices matching a condition