R语言 匹配字符串中的模式 – agrep()函数
R语言中的 agrep() 函数用于在给定字符串的每个元素中搜索与模式近似的匹配。
语法:
agrep(pattern, x, ignore.case=FALSE, value=FALSE)
参数:
pattern: 指定的模式,将与给定的字符串元素进行匹配。
x: 指定的字符串向量。
ignore.case: 如果其值为TRUE,则忽略case。
value: 如果其值为TRUE,则返回匹配元素向量,否则返回索引向量。
例1:
# R program to illustrate
# agrep function
# Creating string vector
x <- c("GFG", "gfg", "Geeks", "GEEKS")
# Calling agrep() function
agrep("gfg", x)
agrep("Geeks", x)
agrep("gfg", x, ignore.case = TRUE)
agrep("Geeks", x, ignore.case = TRUE)
输出 :
[1] 2
[1] 3
[1] 1 2
[1] 3 4
例2:
# R program to illustrate
# agrep function
# Creating string vector
x <- c("GFG", "gfg", "Geeks", "GEEKS")
# Calling agrep() function
agrep("gfg", x, ignore.case = TRUE, value = TRUE)
agrep("G", x, ignore.case = TRUE, value = TRUE)
agrep("Geeks", x, ignore.case = FALSE, value = FALSE)
agrep("GEEKS", x, ignore.case = FALSE, value = FALSE)
输出:
[1] "GFG" "gfg"
[1] "GFG" "gfg" "Geeks" "GEEKS"
[1] 3
[1] 4