rtweet - multiple AND/OR keyword search

Viewed 877

I am using the rtweet package to retrieve tweets that contain specific keywords. I know how to do an "and"/"or" match, but how to chain these together into one keyword query with multiple OR/and conditions . For example, a search query I may wish to put into the search_twitter function is:

('cash' or 'currency' or 'banknote' or 'accepting cash' or 'cashless') AND ('covid' or 'virus' or 'coronavirus')

So the tweets can contain any one of the words in the first bracket and also any one of the words in the second bracket.

1 Answers

Using dplyr:

Assuming you have a df with a column that contains a character field of tweets:

Sample data:

df <- structure(list(Column = c("coronavirus cash", "covid", "currency covid", 
"currency coronavirus", "coronavirus virus", "trees", "plants", 
"moneys")), row.names = c(NA, -8L), class = c("tbl_df", "tbl", 
"data.frame"))

You can use the following:

library(dplyr)

match <- df %>%
  dplyr::filter(str_detect(Column, "cash|currency|banknote|accepting cash|cashless")) %>%
  dplyr::filter(str_detect(Column, "covid|virus|coronavirus"))
Related