在 R 编程中检查向量中的模式 – grepl()函数
R 语言中的grepl()
函数用于返回值 True 如果在向量中找到指定的模式,如果没有找到则返回 false。
Syntax: grepl(pattern, string, ignore.case=FALSE)
Parameters:
pattern: regular expressions pattern
string: character vector to be searched
ignore.case: whether to ignore case in the search. Here ignore.case is an optional parameter as is set to FALSE by default.
示例 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