2

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.

dano_
  • 303
  • 1
  • 8
  • Others may have seen different behavior, but in my experience of downloading millions of tweets with `rtweet`, the package does not behave well with multiple conditions in search queries (It may be an issue with how Twitter treats these queries, rather than an issue with `rtweet`. It may be better to download the options using multiple simple queries and then combine /filter /subset the data to get what you are looking for. – aiatay7n Apr 08 '20 at 04:25

1 Answers1

0

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"))
Matt
  • 7,255
  • 2
  • 12
  • 34