How to remove a character or multiple characters from a string in R? You can use either the R base function gsub()
or use str_replace() from stringr package to remove characters from a string or text.
In this article, I will explain how to remove a single character or multiple characters from a String in R by using gsub()
and str_replace()
functions.
1. Remove Specific Character from String
You can use the gsub() function to remove a character from a string or text in R. This is an R base function that takes 3 arguments, first, the character to look for, second, the value to replace with, in our case we use blank string, and the third input string was to replace.
# Remove Single Character
address_str <- "127 Anton Blvd, Apt #7 - Wilmington, DE"
new_str <- gsub(',','',address_str)
print(new_str)
# Output
# [1] "127 Anton Blvd Apt #7 - Wilmington DE"
2. Remove Multiple Characters from String
gsub() function is also used to remove multiple characters from the String. To look for multiple strings, you have to use []. The below example removes Anton and Apt strings from the String.
# Remove Multiple Characters
address_str <- "127 Anton Blvd, Apt #7 - Wilmington, DE"
new_str <- gsub('[AntonApt]','',address_str)
print(new_str)
# Output
# [1] "127 Blvd, #7 - Wilmig, DE"
3. Remove Special Characters from String
To remove all special characters use ^[:alnum:]
to gsub() function, the following example removes all special characters [that are not a number and alphabet characters] from R data.frame.
# Remove Special Characters
address_str <- "127 Anton Blvd, Apt #7 - Wilmington, DE"
new_str <- gsub('[^[:alnum:] ]','',address_str)
print(new_str)
# Output
# [1] "127 Anton Blvd Apt 7 Wilmington DE"
4. Remove Spaces in a String
Sometimes we would be required to remove spaces from a string or remove tab or newline characters, you can easily achieve this by using the below example.
# Remove space from String
address_str <- "127 Anton Blvd, Apt #7 - Wilmington, DE"
new_str <- gsub(' ','',address_str)
print(new_str)
# Remove tab and new line charater
new_str <- gsub('[\t\n]','',address_str)
print(new_str)
Conclusion
In this article, I have explained how to remove a single character, or multiple characters from the string in R. Also learned to remove special characters, characters by position, and many more examples.
Related Articles
- How to remove rows with NA in R
- How to remove duplicate rows in R
- How to remove rows in R
- How to remove columns in R
- How to remove NA from R Vector
- R Remove Duplicates From Vector
- Uninstall or Remove Package from R Environment
- R Remove From Vector with Examples
- Remove white all space from string in r