0
0
R-programmingHow-ToBeginner · 3 min read

How to Use sub Function in R for String Replacement

In R, use the sub() function to replace the first occurrence of a pattern in a string with a replacement string. The syntax is sub(pattern, replacement, x), where pattern is what you want to find, replacement is what you want to replace it with, and x is the original string.
📐

Syntax

The sub() function has three main parts:

  • pattern: The text or regular expression you want to find.
  • replacement: The text you want to replace the first match with.
  • x: The original string or vector of strings where the replacement happens.

It replaces only the first match found in each string.

r
sub(pattern, replacement, x)
💻

Example

This example shows how to replace the first occurrence of "cat" with "dog" in a string.

r
text <- "The cat sat on the cat mat."
result <- sub("cat", "dog", text)
print(result)
Output
[1] "The dog sat on the cat mat."
⚠️

Common Pitfalls

One common mistake is expecting sub() to replace all matches. It only replaces the first match. To replace all matches, use gsub() instead.

Also, forgetting that pattern can be a regular expression may cause unexpected results if special characters are not escaped.

r
text <- "apple apple apple"
# Wrong: expecting all replaced
sub("apple", "orange", text)
# Right: replace all
result_all <- gsub("apple", "orange", text)
print(result_all)
Output
[1] "orange apple apple" [1] "orange orange orange"
📊

Quick Reference

sub() replaces the first match; gsub() replaces all matches.

  • pattern: text or regex to find
  • replacement: text to insert
  • x: input string(s)

Use fixed = TRUE if you want to match the pattern literally without regex.

Key Takeaways

Use sub() to replace only the first occurrence of a pattern in a string.
For replacing all occurrences, use gsub() instead of sub().
The pattern argument can be a regular expression or plain text.
Set fixed = TRUE to treat the pattern as plain text, not regex.
sub() works on vectors, replacing the first match in each element.