How to Use str_extract in R (With Examples)


The str_extract() function from the stringr package in R can be used to extract matched patterns in a string.

This function uses the following syntax:

str_extract(string, pattern)

where:

  • string: Character vector
  • pattern: Pattern to extract

The following examples show how to use this function in practice.

Example 1: Extract One Pattern from String

The following code shows how to extract the string “ther” from a particular string in R:

library(stringr)

#define string
some_string <- "Hey there my name is Doug"

#extract "ther" from string
str_extract(some_string, "ther")

[1] "ther"

The pattern “ther” was successfully extracted from the string.

Note that if we attempt to extract some pattern that doesn’t exist in the string, we’ll simply receive NA as a result:

library(stringr)

#define string
some_string <- "Hey there my name is Doug"

#attempt to extract "apple" from string
str_extract(some_string, "apple")

[1] NA

Since the pattern “apple” did not exist in the string, a value of NA was returned.

Example 2: Extract Numeric Values from String

The following code shows how to use the regex \\d+ to extract only the numeric values from a string:

library(stringr)

#define string
some_string <- "There are 350 apples over there"

#extract only numeric values from string
str_extract(some_string, "\\d+")

[1] "350"

Example 3: Extract Characters from Vector of Strings

The following code shows how to use the regex [a-z]+ to extract only characters from a vector of strings:

library(stringr)

#define vector of strings
some_strings <- c("4 apples", "3 bananas", "7 oranges")

#extract only characters from each string in vector
str_extract(some_strings, "[a-z]+")

[1] "apples"  "bananas" "oranges"

Notice that only the characters from each string are returned.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Use str_replace in R
How to Use str_split in R
How to Use str_detect in R

Leave a Reply

Your email address will not be published.