chartr() function in R Programming Language is used to do string substitutions. It replaces all the matches of the existing characters of a string with the new characters specified as arguments.
Syntax: chartr(old, new, x)
Parameters:Â
- old: old string to be substituted
- new: new string
- x: target string
Example 1:Â
# R program to illustrate
# chartr function
# Initializing a string
x <- "Geeks GFG"
# Calling the chartr() function
# which will substitute G with Z
# to the above specified string
chartr("G", "Z", x)
Output :Â
[1] "Zeeks ZFZ"
Example 2:Â
# R program to illustrate
# chartr function
# Initializing a string
x <- "GeeksforGeeks Geeks"
# Calling the chartr() function
# which will substitute G with 1,
# k with 2 and s with 3
# to the above specified string
chartr("Gks", "123", x)
Output:Â
[1] "1ee23for1ee23 1ee23"