在 R 编程中寻找元素的独特匹配 – char.expand()函数
R 语言中的char.expand()函数用于在第二个参数的元素中寻找其第一个参数的唯一匹配。如果成功,则返回此元素;否则,它执行第三个参数指定的动作。
Syntax: char.expand(input, target, nomatch = stop(“no match”))
Parameters:
input: character string to be expanded
target: character vector with the values to be matched against
nomatch: an R expression to be evaluated in case expansion was not possible
示例 1:
Python3
# 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"))
Python3
# 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] "and"
[1] "and"
[1] "and"
示例 2:
Python3
# 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