Open In App

Find String Matches in a Vector or Matrix in R Programming - str_detect() Function

Last Updated : 03 Jun, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report
str_detect() Function in R Language is used to check if the specified match of the substring exists in the original string. It will return TRUE for a match found otherwise FALSE against each of the element of the Vector or matrix.
Note: This function uses 'stringr' Library.
Syntax: str_detect(string, pattern) Parameter: string: specified string pattern: Pattern to be matched
Example 1: Python3 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)
Output:
[1]  TRUE FALSE FALSE FALSE
Example 2: Python3 1==
# 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)
Output:
,, 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

Next Article
Article Tags :

Similar Reads