Replace Specific Characters in String in R
Last Updated :
23 Sep, 2021
In this article, we will discuss how to replace specific characters in a string in R Programming Language.
Method 1: Using gsub() function
We can replace all occurrences of a particular character using gsub() function.
Syntax: gsub(character,new_character, string)
Parameters:
- string is the input string
- character is the character present in the string to be replaced
- new_character is the new character to be placed in the place in the existing character
Example: R program to replace character in a string using gsub() function
R
# consider a string "Hello Geek"
# replace the character 'e' in "Hello
# Geek" with "E"
print(gsub("e", "E", "Hello Geek") )
# consider a string "Python and java"
# replace the character 'a' in "Python
# and java" with "M"
print(gsub("a", "M", "Python and java") )
Output:
[1] "HEllo GEEk"
[1] "Python Mnd jMvM"
Method 2: Using sub() function
We can replace only the first occurrence of a particular character using sub() function, it will replace only the first occurrence character in the string
Syntax: sub(character,new_character, string)
Parameters:
- string is the input string
- character is the character present in the string to be replaced
- new_character is the new character to be placed in the place in the existing character
Example: R program to replace character in a string using sub() function
R
# consider a string "Hello Geek"
# replace the character 'e' in "Hello Geek"
# with "E"
print(sub("e", "E", "Hello Geek") )
# consider a string "Python and java"
# replace the character 'a' in "Python and java"
# with "M"
print(sub("a", "M", "Python and java") )
Output:
[1] "HEllo Geek"
[1] "Python Mnd java"
Method 3: Using str_replace_all() function
str_replace_all() is also a function that replaces the character with a particular character in a string. It will replace all occurrences of the character. It is available in stringr package. So, we need to install and load the package
install: install.packages("stringr")
load: library("stringr")
Syntax: str_replace_all(string, "character", "new_character")
Parameters:
- string is the input string
- character is the character present in the string to be replaced
- new_character is the new character to be placed in the place in the existing character
Example: R program to replace character in a string using str_replace_all() function
R
# load the stringr package
library("stringr")
# consider a string "Hello Geek"
# replace the character 'e' in "Hello Geek"
# with "E"
print(str_replace_all( "Hello Geek","e", "E") )
# consider a string "Python and java"
# replace the character 'a' in "Python and java"
# with "M"
print(str_replace_all("Python and java","a", "M") )
Output:
[1] "HEllo GEEk"
[1] "Python Mnd jMvM"
Method 4: Using str_replace() function
str_replace() is also a function that replaces the character with a particular character in a string. It will replace only the first occurrence.
Syntax: str_replace(string, "character", "new_character")
Parameters:
- string is the input string
- character is the character present in the string to be replaced
- new_character is the new character to be placed in the place in the existing character
Example: R program to replace character in a string using str_replace() function
R
# load the stringr package
library("stringr")
# consider a string "Hello Geek"
# replace the character 'e' in "Hello Geek"
# with "E"
print(str_replace( "Hello Geek","e", "E") )
# consider a string "Python and java"
# replace the character 'a' in "Python and java"
# with "M"
print(str_replace("Python and java","a", "M") )
Output:
[1] "HEllo Geek"
[1] "Python Mnd java"
Similar Reads
Remove All Special Characters from String in R In this article, we are going to remove all special characters from strings in R Programming language. For this, we will use the str_replace_all() method to remove non-alphanumeric and punctuations which is available in stringr package. Installation To install this library type the below command in
1 min read
Replace Character Value with NA in R In this article, we are going to see how to replace character value with NA in R Programming Language. We can replace a character value with NA in a vector and in a dataframe. Example 1: Replace Character Value with NA in vector In a vector, we can replace it by using the indexing operation. Syntax:
2 min read
Repeat Character String N Times in R In this article, we will discuss how to repeat the character string N times in the R programming language. Character string means a set of characters . Example: "Hello Geek","Python","Languages_Python" are some examples Method 1: Using replicate() method This function used to give n replicas from th
3 min read
Replace specific values in column using regex in R In this article, we will discuss how to replace specific values in columns of dataframe in R Programming Language. Method 1 : Using sub() method The sub() method in R programming language is a replacement method used to replace any occurrence of a pattern matched with another string. It is operative
3 min read
Remove Newline from Character String in R In this article, we are going to see how to remove the new line from a character string in R Programming Language. Example: Input: String with newline: "Hello\nGeeks\rFor\r\n Geeks" Output: String after removing new line: "HelloGeeksFor Geeks"Method 1: Using gsub() function gsub() function in R Lang
2 min read
Remove All White Space from Character String in R In this article, we are going to see how to remove all the white space from character string in R Programming Language. We can do it in these ways: Using gsub() function.Using str_replace_all() function.Method 1: Using gsub() Function gsub() function is used to remove the space by removing the space
2 min read