Reputation: 4299
I am struggling to properly exclude variables when I use grepl
.
Imagine my vector is
vec = c("POP MUSIC", "TOP THE POPS", "POPEYE", "MARY POPPINS")
I want to grepl
POP
when I visually recognise pop music.
In my example I want then to exclude "POPEYE", "MARY POPPINS"
.
How could I do something like ? And why this line of code does not work ?
vec[ grepl("POP", vec ) & grepl("^POPEY$", vec ) & grepl("^MARY POPPINS$", vec ) ]
desired results
"POP MUSIC" "TOP THE POPS"
thanks
Upvotes: 1
Views: 562
Reputation: 626804
You can use the following grep
solution:
vec = c("POP MUSIC", "TOP THE POPS", "POPEYE", "MARY POPPINS")
grep("(?i)\\bPOPS?\\b", vec, value = TRUE)
See IDEONE demo
The regex (?i)\\bPOPS?\\b
matches a whole word POP
or POPS
in a case-insensitive way (due to (?i)
) and if found, the value is returned (due to value=TRUE
).
You may need to adjust the regex as per your needs (e.g. (?i)\\bPOP(S|PING)?\\b
to also allow popping
).
Upvotes: 1