How to filter rows by a condition in R

· 2 min read · Updated March 13, 2026 · beginner
r filtering dplyr data.table

Filtering rows by condition is one of the most common data manipulation tasks. Here are several ways to do it.

With dplyr

The tidyverse approach uses filter():

library(dplyr)

# Filter where salary is greater than 50000
high_earners <- df %>% 
  filter(salary > 50000)

# Multiple conditions (AND)
recent_high_earners <- df %>% 
  filter(salary > 50000 & year == 2024)

# Multiple conditions with OR
either_condition <- df %>% 
  filter(salary > 50000 | years_experience > 10)

Use %in% for matching multiple values:

# Keep only certain departments
selected_depts <- df %>% 
  filter(department %in% c("Engineering", "Sales", "Marketing"))

With base R

Base R subsets with square brackets:

# Single condition
high_earners <- df[df$salary > 50000, ]

# Multiple conditions (AND)
recent_high_earners <- df[df$salary > 50000 & df$year == 2024, ]

# Multiple conditions with OR
either_condition <- df[df$salary > 50000 | df$years_experience > 10, ]

Use which() to get row indices:

indices <- which(df$salary > 50000)
high_earners <- df[indices, ]

With data.table

The data.table package is extremely fast for large data:

library(data.table)

dt <- as.data.table(df)

# Single condition
high_earners <- dt[salary > 50000]

# Multiple conditions
recent_high_earners <- dt[salary > 50000 & year == 2024]

# %in% operator
selected_depts <- dt[department %in% c("Engineering", "Sales")]

Common Filter Operations

OperationdplyrBase R
Equals====
Not equals!=!=
Greater than>>
Less than<<
And&&
Or``
In%in%%in%
Not!!
Is NAis.na()is.na()
Not NA!is.na()!is.na()

Filtering with NA Values

NA values can cause surprises. Handle them explicitly:

library(dplyr)

# Keep rows where salary is not NA
complete_salaries <- df %>% 
  filter(!is.na(salary))

# Keep rows where salary > 50000 OR salary is NA
with_na <- df %>% 
  filter(salary > 50000 | is.na(salary))

See Also