Loop over multiple character columns to derive a new variable

Solution 1:

dplyr

You may use rowwise and c_across:

testa %>% 
  rowwise() %>% 
  mutate(available = ifelse(any(c_across(a1:a4) == "yellow"), "yes", "no"))

  a1     a2     a3     a4    available
  <chr>  <chr>  <chr>  <chr> <chr>    
1 orange red    black  red   no       
2 red    yellow orange brown yes      

base R

Use apply:

testa$available <- apply(testa, 1, function(x) ifelse(any(x == "yellow"), "yes", "no"))