R语言 为字符串中的模式寻求匹配 – pmatch() 函数
R语言中的 pmatch() 函数用于为作为参数传递的模式寻求匹配。它返回以被搜索的模式开始的字符串。
语法: pmatch(pat, string, nomatch)
参数:
pat: 要搜索的模式向量
string: 要匹配的向量
nomatch: 没有找到匹配时返回
例1 :
# R Program to match pattern in a string
# Creating string vector
x <- c("Geeks", "Books", "geek")
x
# Matching pattern
pmatch("gee", x)
pmatch("Boo", x)
输出
[1] "Geeks" "Books" "geek"
[1] 3
[1] 2
例2 :
# R Program to match pattern in a string
# Creating string vector
x <- c("Geeks", "Books", "geek")
x
# Matching pattern
pmatch(c("Gee", "Boo", "re"), x, nomatch = -1)
输出
[1] "Geeks" "Books" "geek"
[1] 1 2 -1