R语言 寻求元素的唯一匹配 – char.expand()函数
R语言中的 char.expand() 函数用于在其第二个参数的元素中寻求其第一个参数的唯一匹配。如果成功,它会返回这个元素;否则,它会执行第三个参数指定的操作。
语法: char.expand(input, target, nomatch = stop("no match"))
参数:
input: 要展开的字符串
target: 带有要匹配的值的字符向量
nomatch: 要评估的R表达式,以防展开不成功
例子1:
# R program to illustrate
# char.expand function
# Creating string vector
x <- c("sand", "and", "land")
# Calling char.expand() function
char.expand("a", x, warning("no expand"))
char.expand("an", x, warning("no expand"))
char.expand("and", x, warning("no expand"))
输出 :
[1] "and"
[1] "and"
[1] "and"
例2:
# R program to illustrate
# char.expand function
# Creating string vector
x <- c("sand", "and", "land")
# Calling char.expand() function
char.expand("G", x, warning("no expand"))
输出:
[1] NA
Warning message:
In eval(nomatch) : no expand