R语言 检查向量中的模式 – grepl()函数
R语言中的 grepl() 函数用于在向量中找到指定的模式时返回True值,如果没有找到则返回false。
语法: grepl(pattern, string, ignore.case=FALSE)
参数:
pattern: 正则表达式模式
string: 要搜索的字符向量
ignore.case: 搜索时是否忽略大小写。这里ignore.case是一个可选的参数,默认设置为FALSE。
例1 :
# R program to illustrate
# grepl function
# Initializing a character vector
str <- c("GFG", "gfg", "Geek", "Geeks")
# Calling the grepl() function to
# find whether any instance(s) of
# ‘GF’ and 'G' are present in the string
grepl('GF', str, ignore.case ="True")
grepl('G', str, ignore.case ="True")
输出
[1] TRUE TRUE FALSE FALSE
[1] TRUE TRUE TRUE TRUE
例2 :
# R program to illustrate
# grepl function
# Initializing a character vector
str <- c("GFG", "gfg", "Geek", "Geeks")
# Calling the grepl() function to
# find whether any instance(s) of
# ‘gfg’ and 'Geek' are present in the string
grepl('gfg', str)
grepl('Geek', str)
输出
[1] FALSE TRUE FALSE FALSE
[1] FALSE FALSE TRUE TRUE