CHAPTER 11
Beginner
Working with Strings in R
Updated: May 18, 2026
5 min read
# CHAPTER 11
Working with Strings in R
1. Chapter Introduction
Text data is everywhere — customer names, product descriptions, survey responses. This chapter masters string manipulation using both base R functions and the tidyversestringr package, which provides a consistent, intuitive API.
2. Base R String Operations
r
3. stringr Package (Recommended)
r
4. Regular Expressions (Regex)
r
5. Common Mistakes
-
paste()vspaste0():paste()adds a space by default.paste0()has no separator. Always usesep=""explicitly orpaste0()for concatenation without spaces.
-
Regex escaping: In R strings,
\dneeds to be\\dbecause\is R's escape character. Always double-backslash regex patterns:\\d,\\w,\\s.
6. MCQs
Question 1
nchar("Hello") returns?
Question 2
paste0("R", "Studio") returns?
Question 3
gsub() vs sub()?
Question 4
strdetect(x, "\\d") returns?
Question 5
strtrim() removes?
Question 6
strsplit("a,b,c", ",") returns?
Question 7
\\d{4} matches?
Question 8
strpad("7", 3, pad="0") returns?
Question 9
toupper() converts?
Question 10
strextract() returns?
7. Interview Questions
-
Q: What is the difference between
gsub()andsub()in R?
- Q: How do you extract email addresses from a vector of strings?
8. Summary
String essentials:nchar(), toupper/lower(), paste()/paste0(), substr(), gsub(), strsplit(). stringr package: strdetect(), strextract(), strreplaceall(), strtrim(), strc(), str_pad(). Regex in R requires double-backslash escaping (\\d, \\w). Use stringr for consistent, readable string code in tidyverse workflows.