What is the difference between different filtering functions in R and which of them is fastest?

This recipe explains what is the difference between different filtering functions in R and which of them is fastest

Recipe Objective

What is the difference between different filtering functions in R? Which of them is fastest ? select () — Used for filtering out only relevant data from the dataframe. filter () — Filtering on the basis of some condition pipe- %>%- Pipe is the fastest filtering function, it makes execution faster and with fewer errors, it does not save unncessary object but rather makes code more readable in the process. arrange ()- Arrange () , arranges the output in ascending/descending order. This recipe demonstrates an example of different filtering functions in R.

Step 1 - Import necessary library

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

Step 2 - Create a dataframe

df <- data.frame(a = c(10,23,15,37,9), b = c(21,44,26,18,30), classify= c('A','B','A','C','A')) print(df)
 "Output of the line of code is :" 
df <- data.frame(a = c(10,23,15,37,9),
                 b = c(21,44,26,18,30),
                 classify= c('A','B','A','C','A'))
print(df)
   a  b classify
1 10 21        A
2 23 44        B
3 15 26        A
4 37 18        C
5  9 30        A

Step 3 - Apply select()

x <- select(df,a,b) print(x)
 "Output of the line of code is :" 

x <- select(df,a,b)
print(x)
   a  b
1 10 21
2 23 44
3 15 26
4 37 18
5  9 30

Step 4 - Apply filter()

Filter rows on basis of column classify ='a'

x <- filter(df,classify=='A') print(x)
 "Output of the line of code is :"

x <- filter(df,classify=='A')
print(x)
   a  b classify
1 10 21        A
2 15 26        A
3  9 30        A
 

Step 5 - Apply arrange()

x <- arrange(df,classify) print(x)
 "Output of the line of code is :" 

x <- arrange(df,classify)
print(x)
   a  b classify
1 10 21        A
2 15 26        A
3  9 30        A
4 23 44        B
5 37 18        C

Step 6 - Pipeline : Apply %>%

The ususal way of performing a function operation is function(argument) The pipe function works argument %>% function

x <- df %>% select(a) print(x)
 "Output of the line of code is :"
   a
1 10
2 23
3 15
4 37
5  9
 

What Users are saying..

profile image

Gautam Vermani

Data Consultant at Confidential
linkedin profile url

Having worked in the field of Data Science, I wanted to explore how I can implement projects in other domains, So I thought of connecting with ProjectPro. A project that helped me absorb this topic... Read More

Relevant Projects

Personalized Medicine: Redefining Cancer Treatment
In this Personalized Medicine Machine Learning Project you will learn to classify genetic mutations on the basis of medical literature into 9 classes.

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.

PyCaret Project to Build and Deploy an ML App using Streamlit
In this PyCaret Project, you will build a customer segmentation model with PyCaret and deploy the machine learning application using Streamlit.

PyTorch Project to Build a GAN Model on MNIST Dataset
In this deep learning project, you will learn how to build a GAN Model on MNIST Dataset for generating new images of handwritten digits.

Tensorflow Transfer Learning Model for Image Classification
Image Classification Project - Build an Image Classification Model on a Dataset of T-Shirt Images for Binary Classification

Build a Multi Touch Attribution Machine Learning Model in Python
Identifying the ROI on marketing campaigns is an essential KPI for any business. In this ML project, you will learn to build a Multi Touch Attribution Model in Python to identify the ROI of various marketing efforts and their impact on conversions or sales..

Demand prediction of driver availability using multistep time series analysis
In this supervised learning machine learning project, you will predict the availability of a driver in a specific area by using multi step time series analysis.

Build Portfolio Optimization Machine Learning Models in R
Machine Learning Project for Financial Risk Modelling and Portfolio Optimization with R- Build a machine learning model in R to develop a strategy for building a portfolio for maximized returns.

Build Real Estate Price Prediction Model with NLP and FastAPI
In this Real Estate Price Prediction Project, you will learn to build a real estate price prediction machine learning model and deploy it on Heroku using FastAPI Framework.

Learn to Build a Siamese Neural Network for Image Similarity
In this Deep Learning Project, you will learn how to build a siamese neural network with Keras and Tensorflow for Image Similarity.