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


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


str_detect()R语言中的函数用于检查原始字符串中是否存在子字符串的指定匹配项。对于找到的匹配项,它将返回 TRUE,否则针对 Vector 或矩阵的每个元素返回 FALSE。

Note: This function uses 'stringr' Library.
用法: str_detect(string, pattern)

参数:
string:指定字符串
pattern:要匹配的模式

范例1:


# R Program to illustrate 
# the use of str_detect function
  
# Loading library
library(stringr)
  
# Creating vector
x <- c("Geeks", "Hello", "Welcome", "For")
  
# Pattern to be matched
pat <- "Geeks"
  
# Calling str_detect() function
str_detect(x, pat)

输出:

[1]  TRUE FALSE FALSE FALSE

范例2:


# R Program to illustrate 
# the use of str_detect function
  
# Loading library
library(stringr)
  
# Creating vector
x1 <- c("Geeks", "Geeks", "Welcome", "Geeks")
x2 <- c("Geeks", "Hello", "Geeks")
  
result <- array(c(x1, x2), dim = c(2, 2, 2))
  
# Pattern to be matched
pat <- "Geeks"
  
# Printing Matrix
result
  
# Calling str_detect() function
str_detect(result, pat)

输出:

,, 1

     [, 1]    [, 2]     
[1, ] "Geeks" "Welcome"
[2, ] "Geeks" "Geeks",, 2

     [, 1]    [, 2]   
[1, ] "Geeks" "Geeks"
[2, ] "Hello" "Geeks"

[1]  TRUE  TRUE FALSE  TRUE  TRUE FALSE  TRUE  TRUE



相关用法


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