The “str_detect” Function in R

  • Package: stringr

  • Purpose: To detect if a pattern is present in each element of a character vector.

  • General Class: String Manipulation

  • Required Argument(s):

    • string: A character vector in which to detect patterns.

    • pattern: The pattern to be detected.

  • Notable Optional Arguments:

    • ...: Additional arguments influencing the detection process, such as ignore_case to perform case-insensitive matching.

  • Example:

  • # Example usage
    library(stringr)

    words <- c("apple", "banana", "orange", "grape")

    # Check if each word contains the letter "a"
    contains_a <- str_detect(words, pattern = "a")

    print(contains_a)

  • In this example, the str_detect function from the stringr package is used to check if each element of the character vector words contains the letter “a”. The result is a logical vector indicating whether the pattern is present in each element.

Previous
Previous

The “str_subset” Function in R

Next
Next

The “str_replace_all” Function in R