How to Use gsub in R: Replace Text Easily
In R, use
gsub(pattern, replacement, x) to replace all occurrences of pattern in the string x with replacement. It works on character vectors and returns the modified text with all matches replaced.Syntax
The basic syntax of gsub is:
pattern: The text or regular expression you want to find.replacement: The text you want to use instead.x: The character vector or string where replacements happen.- It returns a new character vector with all matches replaced.
r
gsub(pattern, replacement, x)
Example
This example shows how to replace all spaces with dashes in a sentence using gsub.
r
text <- "I love programming in R" new_text <- gsub(" ", "-", text) print(new_text)
Output
[1] "I-love-programming-in-R"
Common Pitfalls
One common mistake is using sub instead of gsub when you want to replace all occurrences. sub replaces only the first match. 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: only first 'apple' replaced wrong <- sub("apple", "orange", text) print(wrong) # Right: all 'apple' replaced right <- gsub("apple", "orange", text) print(right)
Output
[1] "orange apple apple"
[1] "orange orange orange"
Quick Reference
| Argument | Description |
|---|---|
| pattern | Text or regex to find |
| replacement | Text to replace matches with |
| x | Input character vector or string |
| ignore.case | Optional: TRUE to ignore case (default FALSE) |
| perl | Optional: TRUE to use Perl-compatible regex (default FALSE) |
| fixed | Optional: TRUE to match pattern as fixed string (default FALSE) |
| useBytes | Optional: TRUE to perform matching byte-by-byte (default FALSE) |
Key Takeaways
Use gsub() to replace all occurrences of a pattern in a string or vector.
Remember gsub() replaces every match, unlike sub() which replaces only the first.
Patterns can be regular expressions; escape special characters if needed.
You can control case sensitivity with the ignore.case argument.
gsub() returns a new modified string; it does not change the original.