R Core Language Concepts
Estimated reading: 3 minutes 45 views

🧵 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() and grepl()
  • 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

FunctionDescription
toupper(x)Convert to uppercase
tolower(x)Convert to lowercase
toupper("hello")     # "HELLO"
tolower("WORLD")     # "world"

🔍 Pattern Matching with grep() and grepl()

FunctionReturnsUse Case
grep()Index positionsWhere pattern matches
grepl()Logical TRUE/FALSEWhether 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 occurrences
  • sub() 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(), and gsub() for common operations
  • grepl() and grep() are powerful for searching
  • Convert between strings and other types using as.character() and as.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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

R – Strings

Or Copy Link

CONTENTS
Scroll to Top