How can I use str_extract in R? Can you provide some examples?

How can I use str_extract in R? Can you provide some examples?

Str_extract is a function in the R programming language that allows users to extract specific patterns or characters from a given string of text. This can be useful in data cleaning and analysis tasks. To use str_extract, one must specify the pattern or characters to be extracted, as well as the string from which they should be extracted. For example, if we have a string “Hello World!” and we want to extract the word “World”, we can use the str_extract function to specify the pattern as “World” and the string as “Hello World!”. This will return the desired result of “World”. Overall, str_extract is a useful tool for manipulating strings in R and can be used in various applications.

Use str_extract in R (With Examples)


The str_extract() function from the 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

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:

Cite this article

stats writer (2024). How can I use str_extract in R? Can you provide some examples?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-use-str_extract-in-r-can-you-provide-some-examples/

stats writer. "How can I use str_extract in R? Can you provide some examples?." PSYCHOLOGICAL SCALES, 28 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-use-str_extract-in-r-can-you-provide-some-examples/.

stats writer. "How can I use str_extract in R? Can you provide some examples?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-use-str_extract-in-r-can-you-provide-some-examples/.

stats writer (2024) 'How can I use str_extract in R? Can you provide some examples?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-use-str_extract-in-r-can-you-provide-some-examples/.

[1] stats writer, "How can I use str_extract in R? Can you provide some examples?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I use str_extract in R? Can you provide some examples?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top