#5, First Floor, 4th Street , Dr. Subbarayan Nagar Kodambakkam, Chennai-600 024 pro@slogix.in

Office Address

  • #5, First Floor, 4th Street Dr. Subbarayan Nagar Kodambakkam, Chennai-600 024 Landmark : Samiyar Madam
  • pro@slogix.in
  • +91- 81240 01111

Social List

How to split,detect and locate pattern using regular expression in R with stringR package?
Description

To detect pattern using regular expression in R with stringR package

Functions Used

str_detect(string,pattern) – To detect the pattern
str_split(string,pattern) – To split the data base don the pattern
str_locate(string,pattern) – To locate the start and end position of the pattern for the first match
str_locate_all(string,pattern) – To locate the start and end position of the pattern for all matches

Process

  Load the data file

  Convert the data required as a character vector

  Use the predefined function to split,detect and locate pattern

Sapmle Code

library(readtext)
library(stringr)
data #To split the string using a pattern
(str_split(data$text,” “))
#To detect the pattern.Output is a logical vector containing true and false
str_detect(c(“apple”,”bat”,”cat”,”Crypt”,”dog”,”elephant”,”Flag”,”aeiou”,”AEIOU”),”aeiou”)
#To locate the pattern in the string with start and end position for the first match only
str_locate(c(“apple”,”bat”,”cat”,”Crypt”,”dog”,”elephant”,”Flag”,”aeiouaeiou”,”AEIOU”),”aeiou”)
#To locate the pattern in the string with start and end position for all matches
str_locate_all(c(“apple”,”bat”,”cat”,”Crypt-aeiou”,”dog”,”elephant”,”Flag”,”aeiouaeiou”,”AEIOU”),”aeiou”)

Screenshots