I am looking to receive help on creating a binary variable in R 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 that I’m interested in analyzing. I would like to analyze whether texts surrounding animals appear in those columns (to be coded as 1), or if texts surrounding animals don’t appear (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 variable (data_zoo$animals), which indicates whether any of the texts contained mentions of the animals dictionary (1) or did not (0).
I’ve tried writing a code, but I keep getting an error. Here’s what I have so far:
data_zoo <- mutate(data_zoo, data_zoo$animals = if_else(data_zoo$text=="animals_dict", 1, 0))
I would like help with getting the appropriate syntax to create this variable.
user17896 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.