trun ifelse() function into a for loop

It's hard for me to help you without a reprex, but I think you should look at some functions in the package dplyr.

Some combination of mutate(), across(), contains(), and maybe rowwise(). Functions in the stringr package contain better functions for regex matching.

If you'd like more detailed help, try attaching a reprex to this post. If you're unsure what a reprex is, this post will teach you all about them: FAQ: What's a reproducible example (`reprex`) and how do I create one?