Subset Data Frames Using Logical Conditions In R Last Updated : 21 Jul, 2025 Comments Improve Suggest changes Like Article Like Report Subset Data Frames Using Logical Conditions in R programming language means selecting specific rows from a data frame where certain logical conditions are true, such as values being greater than, less than or equal to a given number.1. Subset data frame with ==This method subsets rows where the column value is equal to a specific value. R df <- data.frame( a1 = c(3, 7, 1, 8, 5, 8), a2 = letters[3:8], batch = c("b0", "b1", "b2", "b1", "b3", "b1") ) print(df) print("After subsetting the data frame") df[df$batch == "b1", ] Output:Output2. Subset Data Frame with %in%This method subsets rows where a column's value matches any value in a vector. R df <- data.frame( name = c("a", "b", "c", "d", "e"), id = c(15, 30, 45, 60, 75), batch = c("a0", "b1", "b2", "b1", "c1") ) print(df) print("After subsetting the data frame") res <- df[df$batch %in% c("b1", "c1"), ] print(res) Output:Output3. Subset Data Frame with !=This method subsets rows where the column value is not equal to a specific value. R df <- data.frame( a1 = c(3, 7, 1, 8, 5, 8), a2 = letters[3:8], batch = c("b0", "b1", "b2", "b1", "b3", "b1") ) print(df) print("After subsetting the data frame") df[df$batch != "b1", ] Output:Output4. Subset Using Square Brackets [ ]This is a base R approach to subset rows using logical conditions directly within square brackets. R df <- data.frame( name = c("a", "b", "c", "d", "e"), id = c(15, 30, 45, 60, 75), batch = c("a0", "b1", "b2", "a2", "c1") ) print(df) print("After subsetting the data frame") subset_df <- df[df$id > 30, ] print(subset_df) Output:Output5. Subset Using subset() FunctionThe subset() function is a cleaner and more readable way to apply conditions on data frames. R df <- data.frame( name = c("a", "b", "c", "d", "e"), id = c(15, 30, 45, 60, 75), batch = c("a0", "b1", "b2", "a2", "c1") ) print(df) print("After subsetting the data frame") subset_df <- subset(df, id > 45) print(subset_df) Output:OutputWe learned about how to perform subsetting by using logical conditions. Comment More infoAdvertise with us Next Article Filter Rows Based on Conditions in a DataFrame in R M maheshpe0b68 Follow Improve Article Tags : R Language R Data-science AI-ML-DS With R Data Science R Language +1 More Similar Reads Filter data by multiple conditions in R using Dplyr In this article, we will learn how can we filter dataframe by multiple conditions in R programming language using dplyr package. The filter() function is used to produce a subset of the data frame, retaining all rows that satisfy the specified conditions. The filter() method in R programming languag 3 min read Analyzing Data in Subsets Using R In this article, we will explore various methods to analyze data in subsets using R Programming Language. How to analyze data in the subsetsAnalyzing data encompasses employing diverse methodologies to acquire insights, recognize patterns, and draw significant conclusions from datasets. This encompa 4 min read Split Spark DataFrame based on condition in Python In this article, we are going to learn how to split data frames based on conditions using Pyspark in Python. Spark data frames are a powerful tool for working with large datasets in Apache Spark. They allow to manipulate and analyze data in a structured way, using SQL-like operations. Sometimes, we 5 min read Split Spark DataFrame based on condition in Python In this article, we are going to learn how to split data frames based on conditions using Pyspark in Python. Spark data frames are a powerful tool for working with large datasets in Apache Spark. They allow to manipulate and analyze data in a structured way, using SQL-like operations. Sometimes, we 5 min read Filter Rows Based on Conditions in a DataFrame in R To filter rows in a data frame using R, we can apply conditions directly to the columns. R offers several ways to perform this, depending on whether the condition is single or multiple.1. Filter Rows Based on a Single ConditionThis method filters rows where a specific condition is applied to a singl 2 min read How to plot a subset of a dataframe in R ? In this article, we will learn multiple approaches to plotting a subset of a Dataframe in R Programming Language. Here we will be using, R language's inbuilt "USArrests" dataset. Method 1: Using subset() function In this method, first a subset of the data is created base don some condition, and then 2 min read Like