How to do string manipulation in R?

How to do string manipulation in R?

How to do string manipulation in R?

This recipe helps you do string manipulation in R


Recipe Objective

String or character datatype is constructed by specifying any value in double quotes or single quotes. String manipulation in R include Concatenation, formatting the style, Changing the case and Extracting parts of a string.

This recipe demonstrates Concatenation and Extracting parts of a string.

1. Concatenation

We use paste() function to combine different string values. It takes any number of arguements.


paste(x, sep, collapse)


  1. x = any number of string values that needs to be combined
  2. sep = (optional) any separator between the arguements
  3. collapse = It is used to remove the spaces between two strings/arguements but not the space within two words.

Example: ​

# assigning strings value to three variables x = "Hello" y = "fellow human" z = "I am Siri" ​ ​ # using paste() function print(paste(x,y,z, sep = " ", collapse = NULL)) ​
[1] "Hello fellow human I am Siri"

2. Extraction

We use substring() function to extract parts of the string.


substring(x, first, last)


  1. x = any character vector
  2. first = start position whee extraction needs to take place
  3. end = end position until where exctraction needs to take place


# assigning string value to a variable z = "I am Siri" ​ ​ # using substring() function to extract "am" from z vector print(substring(z, 3,4))
[1] "am"

Relevant Projects

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

Choosing the right Time Series Forecasting Methods
There are different time series forecasting methods to forecast stock price, demand etc. In this machine learning project, you will learn to determine which forecasting method to be used when and how to apply with time series forecasting example.

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.

Build a Similar Images Finder with Python, Keras, and Tensorflow
Build your own image similarity application using Python to search and find images of products that are similar to any given product. You will implement the K-Nearest Neighbor algorithm to find products with maximum similarity.

Build a Music Recommendation Algorithm using KKBox's Dataset
Music Recommendation Project using Machine Learning - Use the KKBox dataset to predict the chances of a user listening to a song again after their very first noticeable listening event.

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.

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.

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.

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.

Topic modelling using Kmeans clustering to group customer reviews
In this Kmeans clustering machine learning project, you will perform topic modelling in order to group customer reviews based on recurring patterns.