當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。