Вам, вероятно, придется рассказать подробнее и привести реальные примеры данных, но в принципе это очень выполнимо.Вот пример, который, я надеюсь, вы найдете полезным:
# here are some 'documents' -- just text strings
doc1 <- "hello. apple horse."
doc2 <- "hello. banana legislature"
doc3 <- "hello, apple banana. horse legislature"
# store them in a list...
list_of_docs <- list(doc1, doc2, doc3)
# ...so we can apply a custom function to this list
lapply(list_of_docs, function(d) {
# split each document on the '.' character
# (fixed=T means interprest this as plain text, not regex)
phrases_in_d <- unlist(strsplit(d, '.', fixed=T))
# now here's a regex pattern to search for:
# apple followed by anything followed by banana,
# OR
# banana followed by anything followed by apple
search_regex <- 'apple.*banana|banana.*apple'
# grepl() returns a logical vector (TRUE or FALSE) to say if there's a match
# for 'search regex' among 'phrases in document d'
# any() returns true if any phrases match
any(grepl(search_regex, phrases_in_d))
})
Результатом, как вы ожидаете, является список false, false, true
.