HappiomHappiom
  • Self-Improvement
  • Relationship
  • AI for Life
  • Apps
  • Tech
  • More
    • Online Diary
    • Glossary
  • Learn
    • Book
    • >> Soft Skills
    • Time Management
    • >> Tech Skills
    • R
    • Linux
    • Python
  • Our Apps
    • Download Diary App
    • Write Your First Diary
    • Login to Online Diary App
    • 100K+ Famous Quotes Site
  • Resources
    • Self-Improvement Guide
      • 21-Days to Self-Improvement
      • Creating a Habit
      • Learn Life Experiences
      • Easily Prioritizing Tasks
      • Learning from Mistakes
      • Doing Regular Exercises
      • Setting Priority for Success
      • Avoiding Common Mistakes
      • Eating Healthy Food Regularly
    • Journaling Guide
      • Online Diary
      • Best Diary Apps
      • Diary Writing Ideas
      • Diary Writing Topics
      • Avoid Writing in Diary
      • Diary Writing as Hobby
      • Reasons to Write a Diary
      • Types of Feelings In Diary
      • Improve Diary Writing Skills
  • Self-Improvement
  • Relationship
  • AI for Life
  • Apps
  • Tech
  • More
    • Online Diary
    • Glossary
  • Learn
    • Book
    • >> Soft Skills
    • Time Management
    • >> Tech Skills
    • R
    • Linux
    • Python
  • Our Apps
    • Download Diary App
    • Write Your First Diary
    • Login to Online Diary App
    • 100K+ Famous Quotes Site
  • Resources
    • Self-Improvement Guide
      • 21-Days to Self-Improvement
      • Creating a Habit
      • Learn Life Experiences
      • Easily Prioritizing Tasks
      • Learning from Mistakes
      • Doing Regular Exercises
      • Setting Priority for Success
      • Avoiding Common Mistakes
      • Eating Healthy Food Regularly
    • Journaling Guide
      • Online Diary
      • Best Diary Apps
      • Diary Writing Ideas
      • Diary Writing Topics
      • Avoid Writing in Diary
      • Diary Writing as Hobby
      • Reasons to Write a Diary
      • Types of Feelings In Diary
      • Improve Diary Writing Skills
Expand All Collapse All
  • R Code Examples
    • R Code to Create and Manipulate Vectors
    • R Code to Work with Data Frames
    • R Code to Handle Factors and Categorical Data
    • Example R Code for Basic Data Visualization with ggplot2
    • R Code to Aggregate Data Using dplyr
    • R Code to Apply Functions with lapply and sapply
    • R Code to Handle Missing Data
    • Example R Code for String Manipulation with stringr
    • R Code to Transform Data with tidyr
    • R Code to Perform ADF Test
    • R Code to Perform Data Import and Export with CSV
    • R Code for Filtering Data
    • R Code for Easily Summarizing Data
    • R Code to Perform Linear Regression for Statistical Analysis
    • R Code to Perform t-tests for Statistical Analysis
    • Example R Code for Time Series Analysis
    • R Code for Doing Web Scraping with Examples
    • R Code to Showcase Geospatial Analysis
    • Example R Code to Filter Multiple Conditions (for Data Manipulation)

R Code for Filtering Data

Data filtering is a fundamental process in data analysis, allowing analysts to extract relevant information from large datasets based on specific criteria or conditions.

Whether working with spreadsheets, databases, or programming languages like R, data filtering enables users to focus on subsets of data that are essential for their analysis or decision-making.

Method 1 – Filter data using subset() function

This method demonstrates a simple way to filter data in R using the subset() function. You can adjust the condition inside the subset() function to filter data based on different criteria, and you can also filter based on multiple conditions using logical operators like & for AND and | for OR.

The following is an example of R code for filtering data.

# Create a sample dataframe
data <- data.frame(
  ID = c(1, 2, 3, 4, 5),
  Name = c("John", "Emma", "Michael", "Sophia", "William"),
  Age = c(25, 30, 22, 35, 28),
  Gender = c("M", "F", "M", "F", "M")
)

# Filtering data where Age is greater than 25
filtered_data <- subset(data, Age > 25)

# Print the filtered data
print(filtered_data)

Let me explain the above code,

  • First, create a dataframe named ‘data’ with columns ID, Name, Age, and Gender. This dataframe contains sample data of individuals with their respective attributes.
  • We use the subset() function in R to filter the data based on a condition. In this example, we’re filtering the data where Age is greater than 25.
  • This creates a new dataframe named ‘filtered_data’ containing only the rows where the Age column satisfies the condition.
  • Finally, print the filtered data to see the result.

Method 2 – Using dplyr package

Another commonly used method for filtering data in R is by using the dplyr package, which provides a more intuitive and efficient way to manipulate data frames.

Let me show you an example code using dplyr.

# Load the dplyr package
library(dplyr)

# Create a sample dataframe
data <- data.frame(
  ID = c(1, 2, 3, 4, 5),
  Name = c("John", "Emma", "Michael", "Sophia", "William"),
  Age = c(25, 30, 22, 35, 28),
  Gender = c("M", "F", "M", "F", "M")
)

# Filtering data where Age is greater than 25 using dplyr
filtered_data <- filter(data, Age > 25)

# Print the filtered data
print(filtered_data)

I’ll also explain the above code to you.

  • Load the dplyr package using the library() function. This package provides a set of functions for data manipulation.
  • Same as before, we create a dataframe named ‘data’ with columns ID, Name, Age, and Gender containing sample data.
  • Use the filter() function from the dplyr package to filter the data. Inside the filter() function, we specify the dataframe (data) and the condition for filtering (Age > 25). This filters the rows where the Age column satisfies the condition.
  • Print the filtered data to see the result.

Using dplyr makes the code more readable and concise compared to base R functions like subset(). Additionally, dplyr provides a set of functions for various data manipulation tasks, making it a powerful tool for data analysis in R.

Related Articles
  • R Code to Transform Data with tidyr
  • Example R Code for String Manipulation with stringr
  • R Code to Handle Missing Data
  • R Code to Apply Functions with lapply and sapply
  • R Code to Aggregate Data Using dplyr
  • Example R Code for Basic Data Visualization with ggplot2

No luck finding what you need? Contact Us

Previously
R Code to Perform Data Import and Export with CSV
Up Next
R Code for Easily Summarizing Data
  • About Us
  • Contact Us
  • Archive
  • Hindi
  • Tamil
  • Telugu
  • Marathi
  • Gujarati
  • Malayalam
  • Kannada
  • Privacy Policy
  • Copyright 2025 Happiom. All Rights Reserved.