How to use the R function gsub() to replace multiple characters with the same character

Here we will give some examples of how to use the function gsub() in R in order to replace multiple characters or symbols with another unique character. It can also be used to replaceĀ  multiple characters or symbols with a blank space and thus removing such characters (or symbols) from your character. The first example, is using gsub() to replace just one character with another character.

# This is an example of substituting the letter d with the letter k in the string "Word"
gsub("d","k","Word") 

Hence the output of R is the string “Work”.

[1] "Work" 

This can be done with every character including symbols.

# This is an example of substituting the symbol £ with the symbol $
gsub("£","$","£343") 

This will output the following:

[1] "$343" 

In order to change multiple characters with one same letter, you must put the characters you want to replace inside square brackets as follows.

# This is an example of substituting all the vowels with the full stop sign in the word "elephant"
gsub("[aeiou]",".","elephant") 

This will output the following:

[1] ".l.ph.nt"
 

In order to remove multiple characters or symbols from a string, you must state the the replacement character to be a blank space as follows.

# This is an example of removing all the vowels from the word "Coding"
gsub("[aeiou]","","Coding") 

The output is:

[1] "Cdng" 

Another example isĀ  to change from currency format to numeric. Note that the function “as.numeric” changes the class of the variable from string to numeric.

# This is an example of change from currency format to numeric format
as.numeric(gsub("[£,]", "", "£2,250.96")) 

The output is the following number:

[1] 2250.96