How to apply multiple filters on multiple columns using multiple conditions in R?

This recipe helps you apply multiple filters on multiple columns using multiple conditions in R

Recipe Objective

How to apply multiple filters on multiple columns using multiple conditions in R? A filter () function is used to filter out specified elements from a dataframe that returns TRUE value for the given condition(s). filter () helps to reduce a huge dataset into small chunks of datasets. **Syntax — filter (data,condition)** This recipe illustrates an example of applying multiple filters.

Step 1 - Import necessary library

install.packages("dplyr") # Install package library(dplyr) # load the package

Step 2 - Create a dataframe

df <- data.frame(student_name = c('U','V','X','Y','Z'), grade = c('AA','CC','DD','AB','BB'), math_marks = c(40,80,38,97,65), eng_marks = c(95,78,36,41,25), sci_marks = c(56,25,36,87,15)) print(df)

"Dataframe is ":
  student_name grade math_marks eng_marks sci_marks
1            U    AA         40        95        56
2            V    CC         80        78        25
3            X    DD         38        36        36
4            Y    AB         97        41        87
5            Z    BB         65        25        15

Step 3 - Apply filter()

Using the & operator (and) for filtering out the required rows print(filter(df,math_marks>40 & eng_marks<75))

"Output of code is ":
  student_name grade math_marks eng_marks sci_marks
1            Y    AB         97        41        87
2            Z    BB         65        25        15

Using the | operator (or) for filtering out the required rows print(filter(df,math_marks>40 | eng_marks<75))

"Output of code is ":
  student_name grade math_marks eng_marks sci_marks
1            V    CC         80        78        25
2            X    DD         38        36        36
3            Y    AB         97        41        87
4            Z    BB         65        25        15

What Users are saying..

profile image

Savvy Sahai

Data Science Intern, Capgemini
linkedin profile url

As a student looking to break into the field of data engineering and data science, one can get really confused as to which path to take. Very few ways to do it are Google, YouTube, etc. I was one of... Read More

Relevant Projects

Build a Autoregressive and Moving Average Time Series Model
In this time series project, you will learn to build Autoregressive and Moving Average Time Series Models to forecast future readings, optimize performance, and harness the power of predictive analytics for sensor data.

Image Classification Model using Transfer Learning in PyTorch
In this PyTorch Project, you will build an image classification model in PyTorch using the ResNet pre-trained model.

Loan Eligibility Prediction in Python using H2O.ai
In this loan prediction project you will build predictive models in Python using H2O.ai to predict if an applicant is able to repay the loan or not.

Stock Price Prediction Project using LSTM and RNN
Learn how to predict stock prices using RNN and LSTM models. Understand deep learning concepts and apply them to real-world financial data for accurate forecasting.

GCP MLOps Project to Deploy ARIMA Model using uWSGI Flask
Build an end-to-end MLOps Pipeline to deploy a Time Series ARIMA Model on GCP using uWSGI and Flask

Time Series Forecasting Project-Building ARIMA Model in Python
Build a time series ARIMA model in Python to forecast the use of arrival rate density to support staffing decisions at call centres.

Many-to-One LSTM for Sentiment Analysis and Text Generation
In this LSTM Project , you will build develop a sentiment detection model using many-to-one LSTMs for accurate prediction of sentiment labels in airline text reviews. Additionally, we will also train many-to-one LSTMs on 'Alice's Adventures in Wonderland' to generate contextually relevant text.

Build Time Series Models for Gaussian Processes in Python
Time Series Project - A hands-on approach to Gaussian Processes for Time Series Modelling in Python

Avocado Machine Learning Project Python for Price Prediction
In this ML Project, you will use the Avocado dataset to build a machine learning model to predict the average price of avocado which is continuous in nature based on region and varieties of avocado.

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.