Skip to content

Instantly share code, notes, and snippets.

@geotheory
Created October 2, 2021 13:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geotheory/b82f26f253a3fd5b695817a0cc31eec5 to your computer and use it in GitHub Desktop.
Save geotheory/b82f26f253a3fd5b695817a0cc31eec5 to your computer and use it in GitHub Desktop.
# String in text (vectorised)
# A Python-style String in Text operator that ignores case. Either x or pattern must be a single value. Returns a logical value or vector.
{\(pattern, x) stringi::stri_detect_regex(x, pattern, case_insensitive = TRUE)} -> `%IN%`
# usage
'dog' %IN% 'the nice dog said hello'
#> [1] TRUE
'dog.*said' %IN% 'the nice dog said hello'
#> [1] TRUE
q = 'dog'
vec = c('nice dog', 'nice cat', 'NICE DOG', 'hey duggee')
q %IN% vec
#> [1] TRUE FALSE TRUE FALSE
q = c('dog', 'cat', 'bird', 'mouse')
str = 'the cat sat on the mat'
q %IN% str
#> [1] FALSE TRUE FALSE FALSE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment