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

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.

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.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

Predict Credit Default | Give Me Some Credit Kaggle
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

Solving Multiple Classification use cases Using H2O
In this project, we are going to talk about H2O and functionality in terms of building Machine Learning models.

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

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.

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.

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.

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.