在 R 编程中替换字符的字符 – chartr()函数
R 编程语言中的chartr()函数用于进行字符串替换。它用指定为参数的新字符替换字符串中现有字符的所有匹配项。
Syntax: chartr(old, new, x)
Parameters:
- old: old string to be substituted
- new: new string
- x: target string
示例 1:
R
# 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)
R
# 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)
输出 :
[1] "Zeeks ZFZ"
示例 2:
R
# 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)
输出:
[1] "1ee23for1ee23 1ee23"