🧵 R Strings – Creating, Manipulating, and Formatting Text in R
🧲 Introduction – What Are Strings in R?
In R, a string is a sequence of characters enclosed in either single (') or double (") quotes. Strings are used to store and manipulate textual data such as names, labels, messages, and more. R treats strings as character vectors, and offers a wide range of functions for formatting, searching, splitting, and combining them.
🎯 In this guide, you’ll learn:
- How to create strings in R
- String manipulation functions (
paste(),substr(),nchar(), etc.) - Pattern matching using
grep()andgrepl() - Handling special characters and escape sequences
✍️ Creating Strings in R
You can create strings using quotes:
name <- "Alice"
greeting <- 'Hello, world!'
Both single and double quotes are acceptable. To include quotes inside a string, use escape characters (\\):
quote <- "She said, \"R is awesome!\""
📐 String Length and Substrings
🔢 nchar() – Count Number of Characters
text <- "Data Science"
nchar(text) # Output: 12
✂️ substr() or substring() – Extract Portions of a String
substr(text, 1, 4) # "Data"
substring(text, 6) # "Science"
🔗 Concatenating Strings
Use paste() or paste0() to join strings.
first <- "Data"
second <- "Science"
paste(first, second) # "Data Science"
paste0(first, second) # "DataScience"
paste() Parameters:
sep: character to insert between strings (default is a space)collapse: used to combine multiple strings into one
words <- c("R", "is", "great")
paste(words, collapse = "-") # "R-is-great"
🔄 Changing Case
| Function | Description |
|---|---|
toupper(x) | Convert to uppercase |
tolower(x) | Convert to lowercase |
toupper("hello") # "HELLO"
tolower("WORLD") # "world"
🔍 Pattern Matching with grep() and grepl()
| Function | Returns | Use Case |
|---|---|---|
grep() | Index positions | Where pattern matches |
grepl() | Logical TRUE/FALSE | Whether pattern exists |
names <- c("Alice", "Bob", "Charlie")
grep("li", names) # 1 and 3
grepl("li", names) # TRUE FALSE TRUE
🔁 Replace Text with gsub()
text <- "R is cool"
gsub("cool", "powerful", text) # "R is powerful"
gsub()replaces all occurrencessub()replaces the first occurrence only
📏 Trim or Remove Whitespace
text <- " Hello R "
trimws(text) # "Hello R"
🧪 Convert to and from Strings
Use as.character() to convert other data types into strings:
x <- 42
as.character(x) # "42"
To convert strings to numbers (if applicable):
as.numeric("99") # 99
📌 Summary – Recap & Next Steps
Strings in R are extremely versatile, supporting formatting, case changes, substring extraction, concatenation, and pattern matching. They’re essential for labeling plots, managing data, creating reports, and working with unstructured text.
🔍 Key Takeaways:
- Use
"text"or'text'to create strings - Use
nchar(),substr(),paste(), andgsub()for common operations grepl()andgrep()are powerful for searching- Convert between strings and other types using
as.character()andas.numeric()
⚙️ Real-World Relevance:
String handling is vital for data cleaning, label generation, report formatting, text mining, and natural language processing (NLP) in R.
❓ FAQs – Strings in R
❓ Can I use single or double quotes for strings in R?
✅ Yes. Both work:
s1 <- "Hello"
s2 <- 'World'
❓ What is the difference between paste() and paste0()?
✅ paste() adds a space by default. paste0() does not:
paste("a", "b") # "a b"
paste0("a", "b") # "ab"
❓ How do I extract a part of a string?
✅ Use substr() or substring():
substr("Science", 1, 3) # "Sci"
❓ How can I check if a string contains a word?
✅ Use grepl():
grepl("data", "data science is fun") # TRUE
❓ How to replace a word inside a string?
✅ Use gsub():
gsub("bad", "good", "This is bad") # "This is good"
Share Now :
