在 R 编程中寻找字符串中模式的匹配项 – pmatch()函数
R 语言中的pmatch()
函数用于寻找作为参数传递的模式的匹配。它返回以正在搜索的模式开头的字符串。
Syntax: pmatch(pat, string, nomatch)
Parameters:
pat: pattern vector to be searched
string: vector to be matched
nomatch: return when no match is found
示例 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