stringr::str_detect()
get_help()
docs
The str_detect()
function is part of the {stringr}
package, which is part of the {tidyverse}
.
We use the str_detect()
function to ask if it is TRUE
or FALSE
that a certain pattern is present in a string or array of strings. A common usage of this function is with dplyr::filter()
to subset rows from tibbles (data frames) that contain (or do not contain) a certain substring or regular expression (a special kind of pattern-matching string).
All {stringr}
functions (and R
itself) are case sensitive, which means uppercase and lowercase letters are viewed as different characters. In other words, searching for “A” will not match “a”, and searching for “a” will not match “A.”
To use this function, you need to either first load the {stringr}
library, or always use the function with stringr::str_detect()
notation.
# Load the library
library(stringr)
# Or, load the full tidyverse:
library(tidyverse)
# Or, use :: notation
::str_detect() stringr
::str_detect("string to search in", "substring to look for in bigger string")
stringr
::str_detect(c("array", "of", "strings", "to", "search", "in"),
stringr"substring to look for in each string in the array")
The examples use the variables shown below, as well as the carnivores
dataset. Learn more about the carnivores
with get_help("carnivores")
.
# Show all variables and datasets used in examples:
# A single string
single_sentence## [1] "No doubt about the way the wind blows."
# An array of strings
fruits## [1] "kiwi fruit" "pomelo" "goji berry" "persimmon" "mulberry" "raspberry"
# A tibble
carnivores## # A tibble: 9 × 4
## name genus awake brainwt
## <chr> <fct> <dbl> <dbl>
## 1 Arctic fox Vulpes 11.5 0.0445
## 2 Cheetah Acinonyx 11.9 NA
## 3 Dog Canis 13.9 0.07
## 4 Gray seal Haliochoerus 17.8 0.325
## 5 Jaguar Panthera 13.6 0.157
## 6 Lion Panthera 10.5 NA
## 7 Northern fur seal Callorhinus 15.3 NA
## 8 Red fox Vulpes 14.2 0.0504
## 9 Tiger Panthera 8.2 NA
# Does the word "way" appear in single sentence?
str_detect(single_sentence, "way")
## [1] TRUE
# Does the capitalized word "Wind" appear in single sentence?
str_detect(single_sentence, "Wind")
## [1] FALSE
# Does the word "berry" appear in each string in fruits?
str_detect(fruits, "berry")
## [1] FALSE FALSE TRUE FALSE TRUE TRUE
# Keep only rows where it is `TRUE` that the word "seal" is in `name`, using dplyr::filter
%>%
carnivores ::filter(str_detect(name, "seal")) dplyr
## # A tibble: 2 × 4
## name genus awake brainwt
## <chr> <fct> <dbl> <dbl>
## 1 Gray seal Haliochoerus 17.8 0.325
## 2 Northern fur seal Callorhinus 15.3 NA
# Keep only rows where it is `FALSE` that the word "fox" is in `name`, using dplyr::filter
# For this, we negate the result of `str_detect()` using `!` to get `FALSE` instead of `TRUE`
%>%
carnivores ::filter(!str_detect(name, "fox")) dplyr
## # A tibble: 7 × 4
## name genus awake brainwt
## <chr> <fct> <dbl> <dbl>
## 1 Cheetah Acinonyx 11.9 NA
## 2 Dog Canis 13.9 0.07
## 3 Gray seal Haliochoerus 17.8 0.325
## 4 Jaguar Panthera 13.6 0.157
## 5 Lion Panthera 10.5 NA
## 6 Northern fur seal Callorhinus 15.3 NA
## 7 Tiger Panthera 8.2 NA