How to apply a filter on a vector in R?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

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

0

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)

where:

  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 using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

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.

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.

Sequence Classification with LSTM RNN in Python with Keras
In this project, we are going to work on Sequence to Sequence Prediction using IMDB Movie Review Dataset​ using Keras in Python.

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

Music Recommendation System Project using Python and R
Machine Learning Project - Work with KKBOX's Music Recommendation System dataset to build the best music recommendation engine.

Build a Collaborative Filtering Recommender System in Python
Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.

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.

Resume parsing with Machine learning - NLP with Python OCR and Spacy
In this machine learning resume parser example we use the popular Spacy NLP python library for OCR and text classification.