I want to create a data frame column based on whether a specific dictionary of terms appears within text data.
I currently have a data frame, where the column 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_dict
dictionary with three terms: "cat, "dog", and "fish". The code I have so far creates a new column in my data frame, but all values appear as 0.
data_zoo <- mutate(data_zoo, animals = if_else(text=="animals_dict", 1, 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.