I have a dataset of character variables:

```
col1 = c("a","b","c")
col2 = c("a","b_a","d")
df = data.frame(col1,col2)

  col1 col2
1    a    a
2    b  b_a
3    c    d

```

I want to create a variable a that flags 1,0 if any value in that row contains the substring "a".

```
  col1 col2 a
1    a    a 1
2    b  b_a 1
3    c    d 0

```

My attempt is below. It doesn't quite do it, as I believe it takes `TRUE` if any value in the dataframe contains the substring, rather than the row.

```
df["a"] = ifelse(any(sapply(df,function(x) str_detect(x,"a")),TRUE),1,0)

```

My thinking was that with an `ifelse` statement, any functions within the `ifelse` statement only evaluates `df[i,]` rather than the entire dataframe where `i` is the row it is looking at. This doesn't seem to be the case.

1. How do I construct the data frame I'm looking for? Note that in my real dataset, there are 100+ columns, so it doesn't make sense to list them all out.
2. Why doesn't ifelse only evaluate row `i` of `df`, rather than the whole `df`?

Note that previous questions only look [at one variable](https://stackoverflow.com/questions/39903376/if-column-contains-string-then-enter-value-for-that-row), I am looking at all variables so this is not a duplicate.