How to apply a filter on a vector in R?

How to apply a filter on a vector in R?

How to apply a filter on a vector in R?

This recipe helps you apply a filter on a vector in R


Recipe Objective

Vector is a type of object or data structure in R-language. They are designed to store multiple values of same data-type. For example: if you want to store different 50 food items for each cuisine, you don't need to create 50 variables for each cuisine but just a vector of length 50 of datatype character.

Note: It can not have a combination of any two datatype. It has to be homogeneous in nature.

This recipe demonstrates how to filter out a vector using substr() function and indexing

Step 1: Creating a character vector

We use combine function "c()" to create a vector

a = c("Adam","Neil", "Shantanu", "Naomi", "Harry")

Step 2: Applying a filter on a vector

We use substr() function to first extract out characters from a character vector.

Syntax: substr(X, start, stop)


  1. X = is a vector ;
  2. start = starting point where we extract character;
  3. stop = ending point where we stop theextraction;
# to get the first character of each element of the vector substr(a,1,1)
'A' 'N' 'S' 'N' 'H'

Now, applying a filter to get the elements starting with letter 'N'

a[substr(a,1,1) == 'N']
'Neil' 'Naomi'

Relevant Projects

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

Data Science Project-TalkingData AdTracking Fraud Detection
Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.

Zillow’s Home Value Prediction (Zestimate)
Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

PySpark Tutorial - Learn to use Apache Spark with Python
PySpark Project-Get a handle on using Python with Spark through this hands-on data processing spark python tutorial.

Data Science Project in Python on BigMart Sales Prediction
The goal of this data science project is to build a predictive model and find out the sales of each product at a given Big Mart store.

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.

Perform Time series modelling using Facebook Prophet
In this project, we are going to talk about Time Series Forecasting to predict the electricity requirement for a particular house using Prophet.

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.

Learn to prepare data for your next machine learning project
Text data requires special preparation before you can start using it for any machine learning project.In this ML project, you will learn about applying Machine Learning models to create classifiers and learn how to make sense of textual data.