1

I'm sure the answer to this probably exists out there somewhere, but I'm struggling to find it.

I have a set of strings that are retail outlet names and I am trying to identify any that match certain patterns

With the below code I can relatively easily subset where the single name of outlet 1 ["example"] appears, but am unable to 'match' outlet 2, which consists of two words separated by a space ["example two"].....

des <- subset(poiSep06,
              grepl("example|example two", tolower(poiSep06$NAME)) == TRUE)

The above brings back all records where the name includes "example", but does not bring back anything for "example two".

Can anyone advise/point me in the direction of something to show me what I'm doing wrong [I'm sure it's pretty simple]. I'm reluctant to manipulate the original NAME values too much as it might lead to erroneous outlets being included.

neilfws
  • 32,751
  • 5
  • 50
  • 63
j.rahilly
  • 33
  • 5
  • 1
    Could could you provide a reproducible example? https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – harre Aug 10 '22 at 10:45
  • Do you mean that code excludes row with `example two` ? i think it must not because it has the word `example` too . – Mohamed Desouky Aug 10 '22 at 11:02

1 Answers1

1

Supposing you have a character vector like string below, here's how you would get different results depending upon what you're looking for:

# An example character vector
string <- c("example","example two", "foo 2", "foo 6", "cat dog")

# Any element with "example" in it
string[grepl("example", string)]
#> [1] "example"     "example two"

# Any element with "example" OR "foo" in it
# two equivalent approaches
string[grepl("example", string) | grepl("foo", string)]
#> [1] "example"     "example two" "foo 2"       "foo 6"
string[grepl("example|foo", string)]
#> [1] "example"     "example two" "foo 2"       "foo 6"

# Match any substring in tomatch
tomatch <- c("example","cat")
string[grepl(paste(tomatch, collapse = "|"), string)]
#> [1] "example"     "example two" "cat dog"

# Any element that is EXACTLY "example" or "example two"
# two equivalent approaches
string[string %in% c("example","example two")]
#> [1] "example"     "example two"
string[(string == "example") | (string == "example two")]
#> [1] "example"     "example two"
socialscientist
  • 3,759
  • 5
  • 23
  • 58