I think you may want something different than what others have posted. I may be wrong but the phrase you used:
'A' occurs, then 'B', then 'C'
Indicates to me you want to check if somethings happen in a particular order.
If this is the case may I suggest that you can make your question more explicit. You provide a MWE example but it could be made more minimal without the need for stringi (which I love as a package) because I doubt your tweets look anything like "ACB"
in reality. Hand making 3-5 strings could accomplish this without loading another package. Also showing your desired output makes the problem more explicit with less need for explanation.
df <- data_frame(var1=c(
"I think A is good But then C.",
"'A' occurs, then 'B', then 'C'",
"and a then lower with b that c will fail",
NA,
"what about A, B, C and another ABC",
"CBA?",
"last null"
))
var <- c('A', 'B', 'C')
library(stringi); library(dplyr)
df%>%
mutate(
count_abc = stringi::stri_count_regex(
var1,
paste(var, collapse = '.*?')
),
indicator = count_abc > 0
)
## var1 count_abc indicator
## 1 I think A is good But then C. 1 TRUE
## 2 'A' occurs, then 'B', then 'C' 1 TRUE
## 3 and a then lower with b that c will fail 0 FALSE
## 4 <NA> NA NA
## 5 what about A, B, C and another ABC 2 TRUE
## 6 CBA? 0 FALSE
## 7 last null 0 FALSE
## or if you only care about the summary compute it directly
df%>%
summarize(
count_abc = sum(stringi::stri_detect_regex(
var1,
paste(var, collapse = '.*?')
), na.rm = TRUE)
)
## count_abc
## 1 3
If I'm wrong my apologies for my misunderstanding.