I am looking to receive help on creating a binary variable in RStudio based on whether a specific dictionary of terms appears within text data. I currently have a data frame, where one of those columns (text) contains different texts. I would like to create a new variable within that data frame which detects whether the words in my dictionary appear in any given text (to be coded as 1), or if the words in my dictionary do not appear in any given text(coded as 0).
I already created an “animals” dictionary (animals_dict) (which contains three terms “cat, “dog”, and “fish”). Essentially, I’d like to create a new binary variable (animals) which indicates whether a given text contains mentions of any of the words in my animals dictionary (1) or not (0). The code I have so far (listed below) creates a new column in my dataframe, but all values appear as 0. I believe the issue is that this code is detecting whether the rows in my text column appear as “animals_dict”, vs. detecting whether the words in my animals dictionary appear in that text at all.
data_zoo <- mutate(data_zoo, animals = if_else(text=="animals_dict", 1, 0))