Select Rows by Condition in R (With Examples)


Data manipulation is a fundamental skill in statistical computing. In the R programming language, selecting or filtering rows based on specific criteria—known as conditional subsetting—is one of the most common tasks. This process allows analysts to isolate relevant observations from a larger data frame for targeted analysis.

This comprehensive guide explores the standard, base R methods for efficiently performing conditional row selection. We will cover techniques ranging from simple single-criteria filtering to complex logic involving multiple conditions and value lists.

Core Methods for Conditional Subsetting

Base R provides powerful indexing capabilities using square brackets ([]) where you supply a logical vector (a set of TRUE/FALSE values) to filter the rows. Here are the three primary patterns used for achieving conditional selection in R:

Method 1: Select Rows Based on One Condition

This is the simplest form, applying a single logical test directly to a column within the data frame using standard comparison operators (==, >, <, !=).

df[df$var1 == 'value', ]

Method 2: Select Rows Based on Multiple Conditions

When you need to satisfy multiple criteria simultaneously, you employ Boolean logic operators, primarily the AND operator (&) or the OR operator (|).

df[df$var1 == 'value1' & df$var2 > value2, ]

Method 3: Select Rows Based on Value in List

To check if a column value matches any item in a predefined vector of acceptable values, we utilize the specialized R’s %in% operator. This is far more efficient than stringing together multiple OR conditions.

df[df$var1 %in% c('value1', 'value2', 'value3'), ]

Establishing the Sample Data Set

To demonstrate these conditional subsetting techniques effectively, we will utilize a simple example data frame containing athletic statistics, featuring variables such as points, assists, and team assignment. This data set will serve as the foundation for all subsequent filtering examples.

The code below constructs and displays this foundational data frame in R, which consists of six observations:

#create data frame
df <- data.frame(points=c(1, 2, 4, 3, 4, 8),
                 assists=c(6, 6, 7, 8, 8, 9),
                 team=c('A', 'A', 'A', 'B', 'C', 'C'))

#view data frame
df

  points assists team
1      1       6    A
2      2       6    A
3      4       7    A
4      3       8    B
5      4       8    C
6      8       9    C

With our data frame df established, we can now proceed to implement the three filtering methods discussed previously to extract meaningful subsets of the data.

Method 1: Selecting Rows Using a Single Condition

The most direct way to filter a data frame is by applying a single logical test to one of its columns. The syntax df$column == 'value' generates a logical vector (a sequence of TRUE/FALSE values) that is then used for row subsetting.

For instance, if we only require observations pertaining to Team ‘A’, we specify the condition df$team == 'A'. The resulting output demonstrates how R cleanly extracts only the matching rows:

#select rows where team is equal to 'A'
df[df$team == 'A', ]

  points assists team
1      1       6    A
2      2       6    A
3      4       7    A

We can also use the inequality operator (!=) to select rows that are specifically not equal to some value. This is useful for excluding specific categories or erroneous entries from the analysis.

#select rows where team is not equal to 'A'
df[df$team != 'A', ]

  points assists team
4      3       8    B
5      4       8    C
6      8       9    C

Method 2: Combining Criteria with Multiple Conditions (Boolean Logic)

Complex data analysis often requires filtering based on interactions between multiple variables. To select rows that satisfy two or more separate conditions simultaneously, we must employ logical operators within the subsetting brackets.

The primary operator for requiring both conditions to be TRUE is the AND operator (&). If we are interested in records where the team is ‘A’ and the player scored more than one point, we combine the criteria:

#select rows where team is equal to 'A' and points is greater than 1
df[df$team == 'A' & df$points > 1, ]

  points assists team
2      2       6    A
3      4       7    A

Notice that the first row (1 point, Team A) is excluded because while the team condition was met, the points condition (points > 1) was false. The use of the & operator guarantees that only records where both expressions evaluate to TRUE are selected. If instead you needed rows where at least one condition was true, you would use the OR operator (|).

Method 3: Filtering Against a Set of Values (The %in% Operator)

A common scenario involves filtering a column based on whether its value is present in a specific list of acceptable values. The %in% operator checks if each element on the left-hand side (the column values) is contained within the vector on the right-hand side (the list of acceptable values).

This method significantly improves code clarity and performance compared to manually linking multiple equality checks with the | operator. In this example, we filter the data frame to include only rows where the team column matches either ‘A’ or ‘C’:

#select rows where team is equal to 'A' or 'C'
df[df$team %in% c('A', 'C'), ]

The resulting subset contains all rows matching ‘A’ or ‘C’, while Team ‘B’ rows are excluded. The %in% operator is indispensable when dealing with categorical variables that have several valid levels you wish to isolate.

Summary and Next Steps in R Subsetting

Mastering conditional selection is key to effective data handling in R. By utilizing base R indexing, you can achieve highly specific filtering using single conditions, complex criteria combined with Boolean operators, or efficient list checking using the %in% operator.

These techniques form the bedrock of data cleaning and preparation workflows. While base R indexing is powerful, analysts often choose to use packages like dplyr, which provides the highly intuitive filter() function for writing conditional statements in a more readable, pipeline-friendly manner.

For those interested in expanding their R programming knowledge, the following tutorials explain how to perform other common operations:

Cite this article

Mohammed looti (2025). Select Rows by Condition in R (With Examples). PSYCHOLOGICAL STATISTICS. Retrieved from https://statistics.arabpsychology.com/select-rows-by-condition-in-r-with-examples/

Mohammed looti. "Select Rows by Condition in R (With Examples)." PSYCHOLOGICAL STATISTICS, 2 Nov. 2025, https://statistics.arabpsychology.com/select-rows-by-condition-in-r-with-examples/.

Mohammed looti. "Select Rows by Condition in R (With Examples)." PSYCHOLOGICAL STATISTICS, 2025. https://statistics.arabpsychology.com/select-rows-by-condition-in-r-with-examples/.

Mohammed looti (2025) 'Select Rows by Condition in R (With Examples)', PSYCHOLOGICAL STATISTICS. Available at: https://statistics.arabpsychology.com/select-rows-by-condition-in-r-with-examples/.

[1] Mohammed looti, "Select Rows by Condition in R (With Examples)," PSYCHOLOGICAL STATISTICS, vol. X, no. Y, ص Z-Z, November, 2025.

Mohammed looti. Select Rows by Condition in R (With Examples). PSYCHOLOGICAL STATISTICS. 2025;vol(issue):pages.

Download Post (.PDF)
Scroll to Top