Let's assume two dataframes: A and B containing data like the following one: Dataframe: A Dataframe: B
ColA1 ColA2 ColB1 ColB2
| Dog | Lion | Lion | Lion
| Lion | Dog | Cat | NA
| Zebra | Tiger | Tiger | Tiger
| Bat | Parrot | Dog | Dog
If an animal of ColB1 exists either in ColA1 or ColA2, then insert into ColB2 the name of this animal from 'ColB2', else insert NA.
Instead of running twice the ifelse function twice:
B$ColB2<- ifelse((B$ColB1 %in% A$ColA1 | B$ColB1 %in% AColA2), "animal from ColA1" , NA)
How could this become shorter? By applying an apply function, can it become faster?