Research breakthrough possible @S-Logix pro@slogix.in

Office Address

  • 2nd Floor, #7a, High School Road, Secretariat Colony Ambattur, Chennai-600053 (Landmark: SRM School) Tamil Nadu, India
  • pro@slogix.in
  • +91- 81240 01111

Social List

How to detect pattern using regular expression in R?

Description

To detect pattern using regular expression in R

Functions Used

grep(pattern,x,ignore.case=FALSE,perl=FALSE,value=FALSE,fixed=FALSE,UseBytes=FALSE,invert=FALSE) – To find the index or value matching the given pattern
grepl(pattern,x,ignore.case=FALSE,perl=FALSE,value=FALSE,fixed=FALSE,UseBytes=FALSE,invert=FALSE) – To get logical value of the character vector which is determined by the pattern(Pattern present – True If not then FALSE)
Pattern – Pattern for matching
X – Data
Ignore.case – True – Case is ignored
False – Matching is case sensitive
Perl – True – Use Perl compatible regex
Value – True – Returns the character vector that contains the given pattern
False – Returns the index of character vector that contains the given pattern
Fixed – True – To match the pattern as it is (as a whole string)
UseBytes – True – Byte-by-byte matching

Process

  Convert the data required as a character vector

  Use the predefined function grep() to detect pattern

Sapmle Code

#To find the index position of the character vector containing the given pattern
grep(“[aeiou]”,c(“apple”,”bat”,”cat”,”Crypt”,”dog”,”elephant”,”Flag”,”aeiou”,”AEIOU”))
#To get the character vector containing the given pattern
grep(“[aeiou]”,c(“apple”,”bat”,”cat”,”Crypt”,”dog”,”elephant”,”Flag”,”aeiou”),value=TRUE)
#To get the logical value(TRUE or FALSE) of the character vector containing the pattern
grepl(“[aeiou]”,c(“apple”,”bat”,”cat”,”Crypt”,”dog”,”elephant”,”Flag”,”aeiou”))
#To match the pattern with case sensitive
grep(“[aeiou]”,c(“apple”,”bat”,”cat”,”Crypt”,”dog”,”elephant”,”Flag”,”aeiou”,”AEIOU”),ignore.case=TRUE)
#To match the given pattern with character vector as it is
grep(“aeiou”,c(“apple”,”bat”,”cat”,”Crypt”,”dog”,”elephant”,”Flag”,”aeiou”,”AEIOU”),fixed = TRUE)
#To return the character vector that do not contain the gven pattern
grep(“[aeiou]”,c(“apple”,”bat”,”cat”,”Crypt”,”dog”,”elephant”,”Flag”,”aeiou”,”AEIOU”),invert = TRUE)

Screenshots
detect pattern using regular expression in R