Open In App

Replace Multiple Letters with Accents in R

In this article, we will discuss how to replace multiple letters with accents using R programming language.

Method 1: Using chartr method

chartr() method in R is used to perform string substitutions. It is also used to perform character translation and unfolding. 



Syntax: chartr(old, new, x)

Parameter : 
old : the string to be substituted
new : the string to substitute with
x : the string to use 



Example: Replace multiple letters with accent




# declare character string
str <- "áéèôîáá"
  
# replace string
chartr("áéèôîáá","aeeoiaa", str)

Output:

[1] aeeoiaa

Method 2: Using gsub method

The gsub() method in base R is used to perform string substitutions and replacements. It is used to replace pattern in strings specified. In case the pattern is not found, the string is returned as it is. 

Syntax: gsub(old, new, x)

Parameter : 
old : the string to be substituted
new : the string to substitute with
x : the string to use 

Example: Replacing multiple letters with accent




# declare character string
str <- "áéèôîáá"
  
# replace string
gsub("áéèôîáá","aeeoiaa",str)

Output:

[1] aeeoiaa
Article Tags :