当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


R语言 agrep()用法及代码示例


R 语言中的 agrep() 函数用于在给定字符串的每个元素内搜索与模式的近似匹配。

用法: 
agrep(pattern, x, ignore.case=FALSE, value=FALSE)
参数:
pattern: Specified pattern which is going to be matched with given elements of the string. 
x: Specified string vector. 
ignore.case: If its value is TRUE, it ignore case. 
value: If its value is TRUE, it return the matching elements vector, else return the indices vector. 
 

范例1:

Python3


# 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:

Python3


# 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




相关用法


注:本文由纯净天空筛选整理自Kanchan_Ray大神的英文原创作品 Matching of patterns in a String in R Programming – agrep() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。