I am trying to make a flag which takes note of where a variable contains one of the words from a given list. I have written an example using the starwars dataset from the dplyr library.
starwars <- starwars
name_of_characters <- c("Luke", "Han", "Jabba", "Anakin")
starwars$flag <- ifelse(starwars$name %in% name_of_characters, "Flag", "")
starwars$flag_2 <- ifelse(grep(name_of_characters, starwars$name), "Flag", "")
So what i am trying to do here is to create a flag column which will identify the characters whose names contain "Luke", "Han", "Jabba" or "Anakin". Where these names exist i want my column to simply say "Flag". I'm sure there is an easier way to do this but i've not been able to find this. For reference i'd like this to be similar to the Find() function in Excel or the Contains function in SAS.