How to apply multiple filters on multiple columns using multiple conditions in R?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

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

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

Relevant Projects

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.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

NLP and Deep Learning For Fake News Classification in Python
In this project you will use Python to implement various machine learning methods( RNN, LSTM, GRU) for fake news classification.

Build OCR from Scratch Python using YOLO and Tesseract
In this deep learning project, you will learn how to build your custom OCR (optical character recognition) from scratch by using Google Tesseract and YOLO to read the text from any images.

Ensemble Machine Learning Project - All State Insurance Claims Severity Prediction
In this ensemble machine learning project, we will predict what kind of claims an insurance company will get. This is implemented in python using ensemble machine learning algorithms.

Census Income Data Set Project - Predict Adult Census Income
Use the Adult Income dataset to predict whether income exceeds 50K yr based on census data.

Machine Learning Project to Forecast Rossmann Store Sales
In this machine learning project you will work on creating a robust prediction model of Rossmann's daily sales using store, promotion, and competitor data.

Identifying Product Bundles from Sales Data Using R Language
In this data science project in R, we are going to talk about subjective segmentation which is a clustering technique to find out product bundles in sales data.

Churn Prediction in Telecom using Machine Learning in R
Estimating churners before they discontinue using a product or service is extremely important. In this ML project, you will develop a churn prediction model in telecom to predict customers who are most likely subject to churn.

Machine learning for Retail Price Recommendation with Python
Use the Mercari Dataset with dynamic pricing to build a price recommendation algorithm using machine learning in Python to automatically suggest the right product prices.