HappiomHappiom
  • Self-Improvement
  • Relationship
  • AI for Life
  • Apps
  • Tech
  • More
    • Online Diary
    • Glossary
  • Learn
    • Book
    • >> Soft Skills
    • Time Management
    • >> Tech Skills
    • R
    • Linux
    • Python
  • Our Apps
    • Download Diary App
    • Write Your First Diary
    • Login to Online Diary App
    • 100K+ Famous Quotes Site
  • Resources
    • Self-Improvement Guide
      • 21-Days to Self-Improvement
      • Creating a Habit
      • Learn Life Experiences
      • Easily Prioritizing Tasks
      • Learning from Mistakes
      • Doing Regular Exercises
      • Setting Priority for Success
      • Avoiding Common Mistakes
      • Eating Healthy Food Regularly
    • Journaling Guide
      • Online Diary
      • Best Diary Apps
      • Diary Writing Ideas
      • Diary Writing Topics
      • Avoid Writing in Diary
      • Diary Writing as Hobby
      • Reasons to Write a Diary
      • Types of Feelings In Diary
      • Improve Diary Writing Skills
  • Self-Improvement
  • Relationship
  • AI for Life
  • Apps
  • Tech
  • More
    • Online Diary
    • Glossary
  • Learn
    • Book
    • >> Soft Skills
    • Time Management
    • >> Tech Skills
    • R
    • Linux
    • Python
  • Our Apps
    • Download Diary App
    • Write Your First Diary
    • Login to Online Diary App
    • 100K+ Famous Quotes Site
  • Resources
    • Self-Improvement Guide
      • 21-Days to Self-Improvement
      • Creating a Habit
      • Learn Life Experiences
      • Easily Prioritizing Tasks
      • Learning from Mistakes
      • Doing Regular Exercises
      • Setting Priority for Success
      • Avoiding Common Mistakes
      • Eating Healthy Food Regularly
    • Journaling Guide
      • Online Diary
      • Best Diary Apps
      • Diary Writing Ideas
      • Diary Writing Topics
      • Avoid Writing in Diary
      • Diary Writing as Hobby
      • Reasons to Write a Diary
      • Types of Feelings In Diary
      • Improve Diary Writing Skills
Expand All Collapse All
  • R Code Examples
    • R Code to Create and Manipulate Vectors
    • R Code to Work with Data Frames
    • R Code to Handle Factors and Categorical Data
    • Example R Code for Basic Data Visualization with ggplot2
    • R Code to Aggregate Data Using dplyr
    • R Code to Apply Functions with lapply and sapply
    • R Code to Handle Missing Data
    • Example R Code for String Manipulation with stringr
    • R Code to Transform Data with tidyr
    • R Code to Perform ADF Test
    • R Code to Perform Data Import and Export with CSV
    • R Code for Filtering Data
    • R Code for Easily Summarizing Data
    • R Code to Perform Linear Regression for Statistical Analysis
    • R Code to Perform t-tests for Statistical Analysis
    • Example R Code for Time Series Analysis
    • R Code for Doing Web Scraping with Examples
    • R Code to Showcase Geospatial Analysis
    • Example R Code to Filter Multiple Conditions (for Data Manipulation)

Example R Code for String Manipulation with stringr

String manipulation is a fundamental skill in data analysis and programming. In R, the `stringr` package offers a powerful set of functions to handle and transform text. These tools make it easier to work with strings efficiently.

The `stringr` package provides functions for common operations like extracting substrings, replacing text, and changing case. For example, you can easily convert a string to uppercase or find the position of a specific pattern within a text. These operations are essential for cleaning and preparing data.

In the following examples, we’ll explore how to use `stringr` for various string manipulation tasks.

From extracting portions of a string to splitting text into words, these examples will help you understand how to leverage `stringr` in your own R projects.

Example Code

library(stringr)

# Original string
text <- "Hello, world! Welcome to string manipulation in R."

# 1. Extract a substring
substring <- str_sub(text, 8, 12)
print(substring) # Output: "world"

# 2. Replace text
replaced_text <- str_replace(text, "world", "universe")
print(replaced_text) # Output: "Hello, universe! Welcome to string manipulation in R."

# 3. Convert to uppercase
uppercase_text <- str_to_upper(text)
print(uppercase_text) # Output: "HELLO, WORLD! WELCOME TO STRING MANIPULATION IN R."

# 4. Find the position of a pattern
position <- str_locate(text, "Welcome")
print(position) # Output: "18 24"

# 5. Split text into words
words <- str_split(text, " ")
print(words) # Output: "Hello," "world!" "Welcome" "to" "string" "manipulation" "in" "R."

Example Output

1. Extract a substring
[1] "world"

2. Replace text
[1] "Hello, universe! Welcome to string manipulation in R."

3. Convert to uppercase
[1] "HELLO, WORLD! WELCOME TO STRING MANIPULATION IN R."

4. Find the position of a pattern
[1] 18 24

5. Split text into words
[1] "Hello," "world!" "Welcome" "to" "string" "manipulation" "in" "R."

Detailed Explanation

  • `str_sub()`: Extracts a substring from the original string. Here, it takes characters from position 8 to 12.
  • `str_replace()`: Replaces the first occurrence of a pattern (“world”) with a new value (“universe”).
  • `str_to_upper()`: Converts the entire string to uppercase.
  • `str_locate()`: Finds the start and end positions of a pattern (“Welcome”) in the string.
  • `str_split()`: Splits the string into individual words based on spaces.

Mastering string manipulation with the `stringr` package can significantly enhance your data processing skills in R. Just by practicing the examples provided, you’ll become more comfortable with common operations like substring extraction, text replacement, and case conversion. These skills are vital for data cleaning, text analysis, and preparing data for further analysis.

For beginners, start by experimenting with simple strings and gradually work up to more complex text data. Familiarize yourself with key functions such as `str_sub()`, `str_replace()`, and `str_split()`.

The more you practice, the more proficient you’ll become in manipulating strings effectively in your R projects.

Related Articles
  • R Code to Transform Data with tidyr
  • R Code to Handle Missing Data
  • R Code to Apply Functions with lapply and sapply
  • R Code to Aggregate Data Using dplyr
  • Example R Code for Basic Data Visualization with ggplot2
  • R Code to Handle Factors and Categorical Data

No luck finding what you need? Contact Us

Previously
R Code to Handle Missing Data
Up Next
R Code to Transform Data with tidyr
  • About Us
  • Contact Us
  • Archive
  • Hindi
  • Tamil
  • Telugu
  • Marathi
  • Gujarati
  • Malayalam
  • Kannada
  • Privacy Policy
  • Copyright 2026 Happiom. All Rights Reserved.