Using a regular expression, you can do the following.
x <- c('I like apples', 'I really like apples', 'I like apples and oranges', 'I like oranges and apples', 'I really like oranges and apples but oranges more') x[grepl('^((?!.*orange).)*apple.*$', x, perl=TRUE)]
The regular expression looks ahead to see if there is a character other than a line break and an orange substring, and if so, a period . will match any character except line break, as it is wrapped in a group, and repeated ( 0 or more times). Then we look for apple and any character except a line break ( 0 or more times). Finally, linear bindings begin and end to ensure that the input signal is consumed.
UPDATE You can use the following if performance is a problem.
x[grepl('^(?!.*orange).*$', x, perl=TRUE)]
source share