StrExtract() function from the DescTools package in R extracts a part of a string, defined as a regular expression. This function uses the following syntax: StrExtract(x, pattern) where: x: a character vector where matches are sought; pattern: character string containing a regular expression.
Syntax
StrExtract(x, pattern, ...)
Parameters
x: It is a character vector where matches are sought, or an object which can be coerced by as.character to a character vector.
pattern: It is a character string containing a regular expression (or character string for fixed = TRUE) to be matched in the given character vector.
Return value
It returns a character vector.
Example
library(DescTools)
state <- "We are Venom"
StrExtract(state, "Venom")
Output
[1] "Venom"
Here’s the breakdown of the code:
- library(DescTools) loads the DescTools package, which provides various descriptive statistics and data analysis functions.
- state <- “We are Venom” assigns the string “We are Venom” to the variable state.
- StrExtract(state, “Venom”) applies the StrExtract function from the DescTools package to the state variable, with the substring “Venom” as the pattern to search for. The function returns the substring “Venom” if found in the original string or NA (i.e., missing value) if it is not found. In this case, since “Venom” is present in the string “We are Venom”, the function will return the substring “Venom”.
Using regex in StrExtract() Function
You can also use regular expressions to specify more complex patterns.
library(DescTools)
state <- "We are Venom"
StrExtract(state, "\\bV\\w*m\\b")
Output
[1] "Venom"
In this example, we extracted the first word that starts with “V” and ends with “m” from the string, and we got the “Venom” as an output.
That’s it.

Krunal Lathiya is a Software Engineer with over eight years of experience. He has developed a strong foundation in computer science principles and a passion for problem-solving. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language.