str_detect
Detect the presence or absence of a pattern in a string.
Vectorised over string
and pattern
.
Equivalent to grepl(pattern, x)
.
See str_which()
for an equivalent to grep(pattern, x)
.
Usage
str_detect(string, pattern, negate = FALSE)
Arguments
- string
Input vector. Either a character vector, or something coercible to one.
- pattern
Pattern to look for.
The default interpretation is a regular expression, as described in stringi::stringi-search-regex. Control options with
regex()
.Match a fixed string (i.e. by comparing only bytes), using
fixed()
. This is fast, but approximate. Generally, for matching human text, you'll wantcoll()
which respects character matching rules for the specified locale.Match character, word, line and sentence boundaries with
boundary()
. An empty pattern, "", is equivalent toboundary("character")
.- negate
If
TRUE
, return non-matching elements.
Value
A logical vector.
See Also
stringi::stri_detect()
which this function wraps,
str_subset()
for a convenient wrapper around
x[str_detect(x, pattern)]
Examples
# NOT RUN {
fruit <- c("apple", "banana", "pear", "pinapple")
str_detect(fruit, "a")
str_detect(fruit, "^a")
str_detect(fruit, "a$")
str_detect(fruit, "b")
str_detect(fruit, "[aeiou]")
# Also vectorised over pattern
str_detect("aecfg", letters)
# Returns TRUE if the pattern do NOT match
str_detect(fruit, "^p", negate = TRUE)
# }
Community examples
## Alternatives ``` ex01 = data.frame(fruits = c("apple", "apple", "orange", "banana", "orange", "orange", "banana", "apple"), taste = c("sweet", "tarty", "tangy", "sweet", "tangy", "tangy", "sweet", "sweet"), qty = c(3, 2, 3, 2, 4, 1, 6, 2)) ``` To find tangy *and* sweet fruits: ``` ex01[str_detect(ex01$taste, "(tan)|(swe)"), ] ```
## Alternatives ``` ex01 = data.frame(fruits = c("apple", "apple", "orange", "banana", "orange", "orange", "banana", "apple"), taste = c("sweet", "tarty", "tangy", "sweet", "tangy", "tangy", "sweet", "sweet"), qty = c(3, 2, 3, 2, 4, 1, 6, 2)) ``` To find tangy *and* sweet fruits: ``` ex01[str_detect(ex01$taste, "(tan)|(swe)"), ] ```
comparison with `grepl`: `str_detect` behaves the same as `grepl` if we use only 2 first arguments (i.e. if we keep all optional parameters as default for `grepl`). These 2 first arguments are switched however. ```r fruit <- c("apple", "banana", "pear", "pinapple") # [1] TRUE identical(str_detect(fruit, "a"), grepl("a",fruit)) # [1] TRUE identical(str_detect(fruit, "^a"), grepl("^a",fruit)) # [1] TRUE identical(str_detect(fruit, "a$"), grepl("a$",fruit)) # [1] TRUE identical(str_detect(fruit, "b"), grepl("b",fruit)) # [1] TRUE identical(str_detect(fruit, "[aeiou]"), grepl("[aeiou]",fruit)) # [1] TRUE ``` An additional feature of `str_detect` is that it is vectorized over the `pattern` argument: ``` str_detect("aecfg", letters) grepl(letters,"aecfg") # [1] TRUE # Warning message: # In grepl(letters, "aecfg") : # argument 'pattern' has length > 1 and only the first element will be used identical(str_detect("aecfg", letters), Vectorize(grepl,"pattern", USE.NAMES=FALSE)(letters,"aecfg")) # [1] TRUE ```
ex01 = data.frame(fruits = c("apple", "apple", "orange", "banana", "orange", "orange", "banana", "apple"), taste = c("sweet", "tarty", "tangy", "sweet", "tangy", "tangy", "sweet", "sweet"), qty = c(3, 2, 3, 2, 4, 1, 6, 2)) #### to select using string matches - the sweet ones ex01[str_detect(ex01$taste, "swe"), ]
ex01 = data.frame(fruits = c("apple", "apple", "orange", "banana", "orange", "orange", "banana", "apple"), taste = c("sweet", "tarty", "tangy", "sweet", "tangy", "tangy", "sweet", "sweet"), qty = c(3, 2, 3, 2, 4, 1, 6, 2)) #### to select using string matches - the sweet ones ex01[str_detect(ex01$taste, "swe"), ]
ex01 = data.frame(fruits = c("apple", "apple", "orange", "banana", "orange", "orange", "banana", "apple"), taste = c("sweet", "tarty", "tangy", "sweet", "tangy", "tangy", "sweet", "sweet"), qty = c(3, 2, 3, 2, 4, 1, 6, 2)) #### to select using string matches - the sweet ones ex01[str_detect(ex01$taste, "swe"), ]