stringr: Working with Text Data in R
In this lesson, you will learn how to work with text (strings) in R using the stringr package.
Text data is everywhere — names, emails, addresses, comments, logs, and messages. stringr makes text manipulation simple, consistent, and readable.
What Is a String?
A string is a sequence of characters enclosed in quotes.
In R, strings are commonly used to store text information such as names, labels, and descriptions.
name <- "Dataplexa"
message <- "Learning R is powerful"
What Is stringr?
stringr is an R package that provides easy-to-use functions for handling strings.
All stringr functions start with str_, making them easy to recognize
and remember.
Installing and Loading stringr
Before using stringr, install and load the package.
install.packages("stringr")
library(stringr)
Finding String Length with str_length()
The str_length() function counts the number of characters in a string.
This is useful when validating passwords, IDs, or text input.
str_length("Dataplexa")
Combining Strings with str_c()
The str_c() function joins multiple strings together.
You can control spacing using the sep argument.
str_c("Welcome", "to", "Dataplexa", sep = " ")
Converting Case with str_to_upper() and str_to_lower()
stringr provides functions to convert text to uppercase or lowercase.
This helps standardize text before analysis.
str_to_upper("dataplexa")
str_to_lower("LEARN R")
Detecting Patterns with str_detect()
The str_detect() function checks whether a string contains a specific pattern.
It returns TRUE or FALSE.
str_detect("learn r programming", "r")
Extracting Text with str_extract()
The str_extract() function pulls out matching text from a string.
This is useful for extracting IDs, codes, or keywords.
str_extract("Order ID: 4589", "\\d+")
Replacing Text with str_replace()
The str_replace() function replaces part of a string with new text.
It is commonly used for cleaning and correcting data.
str_replace("I like Java", "Java", "R")
Removing Extra Spaces with str_trim()
Extra spaces often appear in user input or imported data.
The str_trim() function removes leading and trailing spaces.
str_trim(" Dataplexa Learning ")
Splitting Strings with str_split()
The str_split() function breaks a string into parts.
This is helpful when separating values like CSV strings.
str_split("R,Python,SQL", ",")
Why stringr Is Important
stringr simplifies text handling and reduces errors.
It is widely used in data cleaning, reporting, and text analysis projects.
📝 Practice Exercises
Exercise 1
Find the length of the string "Data Science".
Exercise 2
Combine "Learn" and "R" with a space in between.
Exercise 3
Check if the word "analysis" exists in a sentence.
Exercise 4
Replace "Python" with "R" in a string.
✅ Practice Answers
Answer 1
str_length("Data Science")
Answer 2
str_c("Learn", "R", sep = " ")
Answer 3
str_detect("Data analysis is important", "analysis")
Answer 4
str_replace("I love Python", "Python", "R")
What’s Next?
In the next lesson, you will learn how to work with categorical data using the forcats package.
This will help you manage factor variables more effectively.